简体   繁体   English

AttributeError:“恢复”对象没有属性“ prefetch_related”

[英]AttributeError: 'Resume' object has no attribute 'prefetch_related'

I am trying to understand the use of prefetch_related and select_related for optimization. 我试图了解使用prefetch_related和select_related进行优化。 I learned somewhere in the blog that one of the where to use prefetch and select is, prefetch_related is used for reverse relation and select_related for the forward relation. 我在博客的某个地方了解到,使用预取和选择的地方之一是,prefetch_related用于反向关系,而select_related用于正向关系。 In my case there is Resume model and Education Model. 就我而言,有简历模型和教育模型。 Education model has FK of resume with the related_name for reverse relation instead of writing additional _set when querying. 教育模型具有带有相关名称的FK简历,用于反向关系,而不是在查询时编写其他_set。 I need to list all the education of an requested user with the requested user resume. 我需要在请求的用户简历中列出所有对请求的用户的教育。 I could do this without those optimization techniques as follow 如果没有以下优化技术,我可以做到这一点

education_instance = Resume.objects.get(applicant=request.user).educations.all()

When I tried to use the following instead, I get the error I stated in the title 当我尝试使用以下内容时,出现标题中所述的错误

education_instance = Resume.objects.filter(applicant=request.user).prefetch_related('educations')

Here is my model 这是我的模特

class Resume(models.Model):
    applicant = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, blank=False, null=False, help_text="Full Name")

class Education(models.Model):
    resume = models.ForeignKey(Resume, related_name='educations')
    name = models.CharField(max_length=100, blank=False, null=False, help_text="Name of an institution")

Can anyone make me clear on select_related and prefetch_related with simple layman terms ? 有人能用简单的外行术语使我清楚地了解select_related和prefetch_related吗? I could not understand the issue I am getting 我不明白我遇到的问题

From the Exception that you provided, you are trying to call .prefetch_related() on object Resume , not Resume s QuerySet . 根据您提供的异常,您尝试在对象Resume而不是ResumeQuerySet上调用.prefetch_related()

So, make sure you run 因此,请确保您运行

Resume.objects.filter(applicant=request.user).prefetch_related('educations')

not

Resume.objects.get(applicant=request.user).prefetch_related('educations')

Actually you should use select_related in this case. 实际上,在这种情况下,您应该使用select_related。 As far as i know prefetch_related is used for many to many relations and it makes two queries to retrieve objects while select_related is used for normal fk or one to one relations and it fetched all objects in one query using joins. 据我所知,prefetch_related用于多对多关系,它使两个查询检索对象,而select_related用于正常fk或一对一关系,并且它使用联接在一个查询中获取所有对象。

What's the difference between select_related and prefetch_related in Django ORM? Django ORM中的select_related和prefetch_related有什么区别? Check that post for getting to know about select related and prefetch related. 检查该帖子,以了解有关select和prefetch的信息。

first of all go through this [document of prefetch_related][1] 首先,请阅读此[prefetch_related相关文档] [1]

[1]: https://docs.djangoproject.com/en/1.11/ref/models/querysets/#prefetch-related then you will find that you can not use it like this [1]: https//docs.djangoproject.com/zh-CN/1.11/ref/models/querysets/#prefetch-related,那么您将发现无法像这样使用它

you have to apply on Education modal 你必须申请教育模式

education_instance = Education.objects.filter(resume__applicant == request.user).prefetch_related('resume')

hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM