I have a model with a foreign key field user = models.ForeignKey(User) which links to the User model in django.contrib.auth.models. I’d like to do the following:
ModelName.objects.get(user.username=request.user.username)
I get the following error at the above point in my code:
SyntaxError: keyword can't be an expression
What is the correct syntax for this?
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)