Django-allauth包创建一个模型SocialAccount,向用户提供一个ForeignKey。我无法在我的查询集中预先获取这个信息。
我的模特:
class Placerating(models.Model):
author = models.ForeignKey('auth.User', null=True, on_delete=models.SET_NULL)Django-allauth模型:
class SocialAccount(models.Model):
user = models.ForeignKey(allauth.app_settings.USER_MODEL, on_delete=models.CASCADE)当我试图预取这些数据时:
rating = Placerating.objects.all().prefetch_related('author__socialaccount')我收到以下错误消息:
AttributeError: Cannot find 'socialaccount' on User object, 'author__socialaccount' is an invalid parameter to prefetch_related()有线索吗?谢谢!
发布于 2018-10-29 09:47:51
我得到了答案。
SocialAccount是一个反向外键,因此必须在末尾添加"_set“:
评级= Placerating.objects.all().prefetch_related('author__socialaccount_set')
https://stackoverflow.com/questions/47606422
复制相似问题