簡體   English   中英

如何使用django-allauth預取SocialAccount?

[英]How to prefetch SocialAccount with django-allauth?

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()

有什么線索嗎? 謝謝!

我得到了答案。

SocialAccount是反向外鍵,因此必須在末尾添加“ _set”:

等級= Placerating.objects.all()。prefetch_related('author__socialaccount_set')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM