Skip to content
Advertisement

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:

JavaScript

Settings as follows:

JavaScript

Not sure how to go about correcting this error.. so that it complies with the rest-auth serialisers.

Advertisement

Answer

In django-rest-auth, there is a default serializer for User models :

JavaScript

And here they are serializing django.contrib.auth.User

In your case, you are using a custom user model and you don’t have a username field in your models, so it is giving an error while trying to serialize the field username. So you have to write a serializer for your User model and add a path to your settings:

example:

JavaScript

In settings.py

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement