User Account Details in Django Extended UserModel Serializer

How to access user account details like email address, username, first_name and last_name in django account models.

Advert

There are instances where you want to get the information from django user model to the extended profile serializer. Django doesn't not come with a ready to use solution but its not hard to make one.


class UserAccountSerializer(serializers.ModelSerializer):
  id = serializers.IntegerField(source = 'pk', read_only = True)
  username = serializers.CharField(source = 'user.username', read_only = True)
  email = serializers.CharField(source = 'user.email')
  first_name = serializers.CharField(source = 'user.first_name')
  last_name = serializers.CharField(source = 'user.last_name')

It works by defining a field in serializer and using the source from user object of logged in user.

Comments

Wow ! you have someting to tell us. That's great! Please keep in mind that comments are moderated, we employ rel="nofollow" for links, avoid using a spammy word or a domain in name field, it might end up as a Spam. Thanks for reading.

Last 5 Articles

All Articles >

  1. 9 Ways to Boost the Design of Your Sports Team Website

     
  2. DevOps Tools

     
  3. The Best Marketing Apps to Use with Shopify

     
  4. Tips to Increase Software Development Speed

     
  5. Mitigating Risks In Custom Software Development

     

News Letter

Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.