Hello consider the following simple scenario:
<code>class User(models.Model):
email = ...
fname = ...
lname = ...
class UserPreferences(models.Model):
user = models.OneToOneField(User, to_field="email", ...)
preferences = ...
</code>
<code>class User(models.Model):
email = ...
fname = ...
lname = ...
class UserPreferences(models.Model):
user = models.OneToOneField(User, to_field="email", ...)
preferences = ...
</code>
class User(models.Model):
email = ...
fname = ...
lname = ...
class UserPreferences(models.Model):
user = models.OneToOneField(User, to_field="email", ...)
preferences = ...
Now the UserPreferences
table in the db contains id, preferences, user_id.
I want to get the preferences of a user by email.
I want to use RetrieveAPIView.
How to do this?
What is the role of to_field
if it stores the id of user anyway?
I tried the following:
<code>class UserPreferencesRetrieveView(generics.RetrieveAPIView):
serializer_class = UserPreferencesSerializer
queryset = UserPreferences.objects.all()
lookup_field = "email"
</code>
<code>class UserPreferencesRetrieveView(generics.RetrieveAPIView):
serializer_class = UserPreferencesSerializer
queryset = UserPreferences.objects.all()
lookup_field = "email"
</code>
class UserPreferencesRetrieveView(generics.RetrieveAPIView):
serializer_class = UserPreferencesSerializer
queryset = UserPreferences.objects.all()
lookup_field = "email"
I got the error:
<code>Cannot resolve keyword 'email' into field. Choices are: id, preferences, user, user_id
</code>
<code>Cannot resolve keyword 'email' into field. Choices are: id, preferences, user, user_id
</code>
Cannot resolve keyword 'email' into field. Choices are: id, preferences, user, user_id
New contributor
Ahmad Mustapha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.