What is `related_name` used for in Django?

前端 未结 6 1368
长发绾君心
长发绾君心 2020-11-22 03:53

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is

6条回答
  •  死守一世寂寞
    2020-11-22 04:35

    prefetch_related use for prefetch data for Many to many and many to one relationship data. select_related is to select data from a single value relationship. Both of these are used to fetch data from their relationships from a model. For example, you build a model and a model that has a relationship with other models. When a request comes you will also query for their relationship data and Django has very good mechanisms To access data from their relationship like book.author.name but when you iterate a list of models for fetching their relationship data Django create each request for every single relationship data. To overcome this we do have prefetchd_related and selected_related

提交回复
热议问题