[Answered ]-How do I pull a User object based on the attribute of a model it is associated with?

2👍

In general I recommend explicitly giving a related_name to the OneToOneField, e.g.: user = models.OneToOneField(User, related_name='profile'). Without it, by default, the related name is userprofile as you can see from the error message.

You can query it using the <related_field_name>__<field_name_on_related_lookup> field lookup syntax, i.e.:

user = User.objects.select_related('UserProfile').get(profile__slug=user_name_slug)

Learn more at Lookups that span relationships

👤zsepi

Leave a comment