What is `related_name` used for in Django?

前端 未结 6 1426
长发绾君心
长发绾君心 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:33

    The related name parameter is actually an option. If we do not set it, Django automatically creates the other side of the relation for us. In the case of the Map model, Django would have created a map_set attribute, allowing access via m.map_set in your example(m being your class instance). The formula Django uses is the name of the model followed by the string _set. The related name parameter thus simply overrides Django’s default rather than providing new behavior.

提交回复
热议问题