I am following the tutorial on Django Rest Framework – Tutorial 3 Class based views. How to add an url field (pointing to the current snippet) to a serializer? serializers.py urls.py Actual output Desired output Answer You have to use HyperlinkedModelSerializer serializer and HyperlinkedIdentityField fi…
Tag: django-rest-framework
Django rest framework override page_size in ViewSet
I am having problem with django rest framework pagination. I have set pagination in settings like – Below is my viewset. I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. An…
I want my django rest framework serializer to accept input but not add to model
I removed some fields from my model, but I want the serializer to still accept the fields as input. How do I have fields the serializer accepts but doesn’t use? Answer From http://www.django-rest-framework.org/api-guide/serializers/ You can add extra fields to a ModelSerializer or override the default f…
Field name `username` is not valid for model
I am attempting to use rest-auth supplied serialisers to GET (*with headers) user details from the defined endpoint /rest-auth/user/ (*with headers (Content-Type: application/json Authorization: Token 1a5472b2af03fc0e9de31fc0fc6dd81583087523 )) I am getting the following traceback: https://dpaste.de/oYay#L I …
Django Rest Framework Database Error Exception Handling
Is there any way to have Django Rest Framework automatically respond with HTTP_400_STATUS’s when there are database exceptions? (IntegrityError and so on) Example: I have a model with a unique username field and I’m trying to use a generic rest_framework.ListCreateAPIView. HTTP_400_STATUS’s …
Django rest framework serializing many to many field
How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of tags associated with it. models.py serializers.py views.py Answer You will need a TagSerializer, whose class Meta has model = Tag. After…
Overriding Django-Rest-Framework serializer is_valid method
I have a quick question about overriding is_valid. Self is a rest_framework.serializers.ModelSerializer. I’m trying to figure out if there is a better way to modify internal data than reading/writing to the data._kwargs property. I know I can get the data pre-validation via a self.get_initial() call. Bu…
When are create and update called in djangorestframework serializer?
I’m currently implementing djangorestframework for my app RESTful API. After playing around with it, I still do not clearly understand what .create(self, validated_data) and .update(self, validated_data) used for in the serializer. As I understand, CRUD only calls the 4 main methods in viewsets.ModelVie…
How to display all model fields with ModelSerializer?
models.py: serializers.py: So, here I want to use all fields. But I have an error: Field name producer_id is not valid for model Car. How to fix that? Thanks! Answer According to the Django REST Framework’s Documentation on ModelSerializers: By default, all the model fields on the class will be mapped t…
How to run tests django rest framework tests?
I’m learning django rest framework. I wrote a simple test like this: my api returns empty json array for that request. What i don’t know is, how do i run this test? when i use this command: i get as output. It’s not written in documentation to how to run the tests. Answer I believe your test…