Answer the question
In order to leave comments, you need to log in
How to use select related in Django with an existing object?
I have an existing user object which has many related fields via select_related
and prefetch_related
:
user = models.User.objects.first().select_related(...).prefetch_related(...)
Article
(not through user.article_set
). Model Article
has ForeignKey
per model User
:articles = models.Article.objects.filter(user=user)
queryset
y to Article
have an existing object user
, i.e. do something like this:articles = models.Article.objects.filter(user=user).select_related(user)
articles = models.Article.objects.filter(user=user).annotate(user=user)
articles
and assigning everything to them user'a
is not very concise.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question