Django prefetch_select和反向连接问题

时间:2017-02-17 03:55:48

标签: python django orm

我正在与Django进行多次加入。

questions = Question.objects.select_related(
    'publishedquestionmapping__library_question'
).prefetch_related(
    'publishedquestionmapping__library_question__contributor_set__user', 
).filter(
    hide=False).order_by('-pub_date')[:count]

for question in questions:
    contributers = question.publishedquestionmapping.library_question.contributor_set.all() 
    for contributer in contributers:
        print contributer.user

错误为RelatedObjectDoesNotExist: Contributor has no user.

问题是我可以使用prefetch_related ...contributor_set__user,但我不能使用contributer.user?

btw,print contributer.user_id没问题。

此模特贡献者

class Contributor(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)

我想做contributer.user.username。我该怎么做?!

谢谢!

0 个答案:

没有答案