Django Rest Model serializers and model inheritance

advertisements

I write custom model of user in my app. Application components are:

  • Django 1.8
  • Django REST framework 3.2.4
  • Django-allauth 0.23.0
  • Django-rest-auth 0.5.

Code of user model you can see pythonfiddle.com. When I go to Django-rest-auth registration endpoint I see this error:

ValueError at /rest-auth/registration/
Cannot use ModelSerializer with Abstract Models.

This is settings are relating with Django-rest-auth:

AUTH_USER_MODEL='authservice.MyUser'
ACCOUNT_USER_MODEL_USERNAME_FIELD='email'
ACCOUNT_AUTHENTICATION_METHOD='email'
ACCOUNT_EMAIL_VERIFICATION='mandatory'
ACCOUNT_EMAIL_REQUIRED=True
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS=2

How can I solve this problem ?


to the best of my knowledge, DRF does not currently support the use of ModelSerializer with Abstract Models, however, you can use Serializer.

A simple implementation would be something (not tested) like:

class MyUserSerializer(serializers.Serializer):
    email = serializers.EmailField()
    is_active = serializers.BooleanField(default=False)
    is_admin = serializers.BooleanField(default=False)
    student  = serializers.BooleanField(default=True)

Then in your view, you can manually save. Something like...

class MyUserView(APIView):
    def post(self, request):
        serializer = MyUserSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Also, for the discussion about whether or not to include this use of ModelSerializer checkout https://github.com/tomchristie/django-rest-framework/issues/2630 and https://github.com/chibisov/drf-extensions/issues/84

Hope this helps!