Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Override Djoser base endpoint users/me

I am a bit new to Djoser and what i am wanting to do is when an authenticated user naviates to the users/me API endpoint it returns with the user id email and name. I would like for it to also return custom information how would I do that?

like image 600
AnitasWill Avatar asked Dec 20 '25 09:12

AnitasWill


1 Answers

While this might only answer half of your question, if you use a custom user, then you would add a REQUIRED_FIELDS = ['field_you_want_included']

E.g this is how one of my custom user profiles look like.

class CustomUser(AbstractUser):
    username = None
    user_id = models.UUIDField(default=uuid4, primary_key=True, editable=False)
    email = models.EmailField(_('email address'), unique=True)
    receive_news = models.BooleanField(null=True, blank=True, default=False)
    birth_date = models.DateField(null=True, blank=True)
    is_premium = models.BooleanField(default=True)

    USERNAME_FIELD = 'email'
    # FIELDS HERE WILL BE ADDED TO USERS/ME ENDPOINT
    REQUIRED_FIELDS = ['is_premium']
    objects = UserAccountManager()

There is plenty if info on making custom users with djoser, so what matters here is the required_fields list. Now, when making a get to users/me it will also include 'is_premium'. However, for more complex inclusions I would attempt to override the view & serializer. Have not tried that myself but I will edit this post if I do.

like image 80
Thorvald Avatar answered Dec 22 '25 02:12

Thorvald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!