Skip to content
Advertisement

Tag: django-rest-framework

How to add an url field to a serializer with Django Rest Framework

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 field From Django Rest Framework documentation The HyperlinkedModelSerializer class is similar to the ModelSerializer class except

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. Any idea where I am wrong ? Answer I fixed

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 fields by declaring fields on the class, just as you would for a Serializer class.

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 have defined custom user model (using email rather than username)as such: Settings as follows: Not sure how to go about correcting this error.. so that

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 are normally thrown automatically if serializer validation fails but this is actually valid input, just not valid in the

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.ModelViewSet: create(), retrive(), update(), and destroy(). I also have already tried to debug and print out stuff to

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 methods need to

Advertisement