When are create and update called in djangorestframework serializer?

You really must split things between the views and the serializer. Serializers The Serializer is a standalone object. It is used for converting a Django model (or any kind of python datastructure, actually) into a serialized form, and the other way around. You may use it as such, wherever you want. It does not even … Read more

django rest framework queryset doesn’t order

If your model does have an ordering it really will be reflected in the list view by default. I’d suggest overriding get_queryset() and debugging the return result there, or else explicitly adding the ordering to the queryset. For example: queryset = Invoice.objects.all().order_by(‘-published_date’) Wondering if it’s possible you’ve configured a filter that’s overriding the ordering. Worth … Read more

django REST framework – limited queryset for nested ModelSerializer?

In your View Set you may specify the queryset like follows: from rest_framework import serializers, viewsets class MyModelSerializer(serializers.ModelSerializer): class Meta: model = MyModel class MyModelViewSet(viewsets.ModelViewSet): queryset = MyModel.objects.all()[:500] serializer_class = MyModelSerializer I think what you are looking for is the SerializerMethodField. So your code would look as follows: class ContainerSerializer(serializers.ModelSerializer): items = SerializerMethodField(‘get_items’) class Meta: … Read more

Django rest-auth allauth registration with email, first and last name, and without username

Make sure you have ACCOUNT_USERNAME_REQUIRED = False in your settings.py file. For first_name and last_name you need to write a custom RegisterSerializer (https://github.com/iMerica/dj-rest-auth/blob/bf168d9830ca2e6fde56f83f46fe48ab0adc8877/dj_rest_auth/registration/serializers.py#L197) here’s a sample code for serializers.py from allauth.account import app_settings as allauth_settings from allauth.utils import email_address_exists from allauth.account.adapter import get_adapter from allauth.account.utils import setup_user_email class RegisterSerializer(serializers.Serializer): first_name = serializers.CharField(required=True, write_only=True) last_name = … Read more

Django rest framework permission_classes of ViewSet method

I think there is no inbuilt solution for that. But you can achieve this by overriding the get_permissions method: from rest_framework.permissions import AllowAny, IsAdminUser class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer permission_classes_by_action = {‘create’: [AllowAny], ‘list’: [IsAdminUser]} def create(self, request, *args, **kwargs): return super(UserViewSet, self).create(request, *args, **kwargs) def list(self, request, *args, **kwargs): return super(UserViewSet, … Read more

Why does Django REST Framework provide different Authentication mechanisms

Django Rest Framework does not perform authentication in middleware by default for the same reason that Django does not perform authentication in middleware by default: middleware applies to ALL views, and is overkill when you only want to authenticate access to a small portion of your views. Also, having the ability to provide different authentication … Read more

How to use TokenAuthentication for API in django-rest-framework

“how can I send the token with post request to my api” From the docs… For clients to authenticate, the token key should be included in the Authorization HTTP header. The key should be prefixed by the string literal “Token”, with whitespace separating the two strings. For example: Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b “at api side how … Read more

Django Rest Framework – How to test ViewSet?

I think I found the correct syntax, but not sure if it is conventional (still new to Django): def test_view_set(self): request = APIRequestFactory().get(“”) cat_detail = CatViewSet.as_view({‘get’: ‘retrieve’}) cat = Cat.objects.create(name=”bob”) response = cat_detail(request, pk=cat.pk) self.assertEqual(response.status_code, 200) So now this passes and I can assign request.user, which allows me to customize the retrieve method under CatViewSet … Read more

Django REST framework: Check user is in group

The sensible way to parameterize permission classes is to put the parameters on the view class. That’ll let you change the behaviour from view to view. Here’s an example: # permissions.py from django.contrib.auth.models import Group from rest_framework import permissions def is_in_group(user, group_name): “”” Takes a user and a group name, and returns `True` if the … Read more