<tfoot id='Vo6jg'></tfoot>

      <i id='Vo6jg'><tr id='Vo6jg'><dt id='Vo6jg'><q id='Vo6jg'><span id='Vo6jg'><b id='Vo6jg'><form id='Vo6jg'><ins id='Vo6jg'></ins><ul id='Vo6jg'></ul><sub id='Vo6jg'></sub></form><legend id='Vo6jg'></legend><bdo id='Vo6jg'><pre id='Vo6jg'><center id='Vo6jg'></center></pre></bdo></b><th id='Vo6jg'></th></span></q></dt></tr></i><div id='Vo6jg'><tfoot id='Vo6jg'></tfoot><dl id='Vo6jg'><fieldset id='Vo6jg'></fieldset></dl></div>
    1. <legend id='Vo6jg'><style id='Vo6jg'><dir id='Vo6jg'><q id='Vo6jg'></q></dir></style></legend>

        <bdo id='Vo6jg'></bdo><ul id='Vo6jg'></ul>

      <small id='Vo6jg'></small><noframes id='Vo6jg'>

      related_name 是做什么用的?

      时间:2023-08-30
        <tbody id='6lrDJ'></tbody>

      <small id='6lrDJ'></small><noframes id='6lrDJ'>

    2. <i id='6lrDJ'><tr id='6lrDJ'><dt id='6lrDJ'><q id='6lrDJ'><span id='6lrDJ'><b id='6lrDJ'><form id='6lrDJ'><ins id='6lrDJ'></ins><ul id='6lrDJ'></ul><sub id='6lrDJ'></sub></form><legend id='6lrDJ'></legend><bdo id='6lrDJ'><pre id='6lrDJ'><center id='6lrDJ'></center></pre></bdo></b><th id='6lrDJ'></th></span></q></dt></tr></i><div id='6lrDJ'><tfoot id='6lrDJ'></tfoot><dl id='6lrDJ'><fieldset id='6lrDJ'></fieldset></dl></div>
        <bdo id='6lrDJ'></bdo><ul id='6lrDJ'></ul>
        <tfoot id='6lrDJ'></tfoot>

        1. <legend id='6lrDJ'><style id='6lrDJ'><dir id='6lrDJ'><q id='6lrDJ'></q></dir></style></legend>

                本文介绍了related_name 是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                related_name 参数对 ManyToManyFieldForeignKey 字段有什么用处?比如给定下面的代码,related_name='maps'的作用是什么?

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

                class Map(db.Model):
                    members = models.ManyToManyField(User, related_name='maps',
                                                     verbose_name=_('members'))
                

                推荐答案

                related_name 属性指定从 User 模型返回到您的模型的反向关系的名称.

                The related_name attribute specifies the name of the reverse relation from the User model back to your model.

                如果你没有指定 related_name,Django 会自动使用你的模型名称和后缀 _set 创建一个,例如 User.map_set.全部().

                If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all().

                如果您指定,例如related_name=mapsUser 模型上,User.map_set 仍然有效,但 User.maps. 语法显然更干净,不那么笨重;例如,如果您有一个用户对象 current_user,您可以使用 current_user.maps.all() 来获取 Map 的所有实例与 current_user 相关的模型.

                If you do specify, e.g. related_name=maps on the User model, User.map_set will still work, but the User.maps. syntax is obviously a bit cleaner and less clunky; so for example, if you had a user object current_user, you could use current_user.maps.all() to get all instances of your Map model that have a relation to current_user.

                Django 文档有更多详情.

                这篇关于related_name 是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                1. <tfoot id='0gxmV'></tfoot>

                  <legend id='0gxmV'><style id='0gxmV'><dir id='0gxmV'><q id='0gxmV'></q></dir></style></legend>
                  <i id='0gxmV'><tr id='0gxmV'><dt id='0gxmV'><q id='0gxmV'><span id='0gxmV'><b id='0gxmV'><form id='0gxmV'><ins id='0gxmV'></ins><ul id='0gxmV'></ul><sub id='0gxmV'></sub></form><legend id='0gxmV'></legend><bdo id='0gxmV'><pre id='0gxmV'><center id='0gxmV'></center></pre></bdo></b><th id='0gxmV'></th></span></q></dt></tr></i><div id='0gxmV'><tfoot id='0gxmV'></tfoot><dl id='0gxmV'><fieldset id='0gxmV'></fieldset></dl></div>

                  <small id='0gxmV'></small><noframes id='0gxmV'>

                      <tbody id='0gxmV'></tbody>

                      • <bdo id='0gxmV'></bdo><ul id='0gxmV'></ul>