[Django]-How to use 'get' method with foreign key field?

4👍

Try ModelName.objects.get(user__username=request.user.username)

Edit: depending on your model relationship, this may result in multiple records, in which case get() would throw an exception, so you could use this instead:
ModelName.objects.filter(user__username=request.user.username)

1👍

Another convenient way to retrieve objects for a specific user is this

user = request.user
objs = user.modelname_set.get(pk=x)
👤tback

Leave a comment