[Answer]-How to get django model relations using JSON?

1👍

This isn’t really a question about JSON.

Surely the token is associated with a user ID? I don’t use django-rest-framework, but the documentation for TokenAuthentication is pretty clear that once the user logs in with their token, you’ll get a normal auth.User instance in request.user just like you would with a standard web-based login.

Your second question is probably made irrelevant by that, but even so, you can always query by an email address, without needing to loop through:

Manager.objects.get(email=my_email_address)

Again this is standard Django querying – if you’re not familiar with that syntax, you should do the Django tutorial.

Of course, since you have a User already, you can do a more efficient foreign key lookup:

Manager.objects.get(user=request.user)

or even

request.user.manager

(assuming you have a one-to-one relationship from User to Manager – it would have been helpful to see your models).

Leave a comment