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

<legend id='0pFrC'><style id='0pFrC'><dir id='0pFrC'><q id='0pFrC'></q></dir></style></legend>

          <bdo id='0pFrC'></bdo><ul id='0pFrC'></ul>
      1. <tfoot id='0pFrC'></tfoot>
        <i id='0pFrC'><tr id='0pFrC'><dt id='0pFrC'><q id='0pFrC'><span id='0pFrC'><b id='0pFrC'><form id='0pFrC'><ins id='0pFrC'></ins><ul id='0pFrC'></ul><sub id='0pFrC'></sub></form><legend id='0pFrC'></legend><bdo id='0pFrC'><pre id='0pFrC'><center id='0pFrC'></center></pre></bdo></b><th id='0pFrC'></th></span></q></dt></tr></i><div id='0pFrC'><tfoot id='0pFrC'></tfoot><dl id='0pFrC'><fieldset id='0pFrC'></fieldset></dl></div>
      2. 使模型中的用户默认为当前用户

        时间:2023-11-07

              <tbody id='8YWqh'></tbody>

            <small id='8YWqh'></small><noframes id='8YWqh'>

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

              <bdo id='8YWqh'></bdo><ul id='8YWqh'></ul>

                <tfoot id='8YWqh'></tfoot>
                  本文介绍了使模型中的用户默认为当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下几点:

                  from django.contrib.auth.models import User
                  
                  class ClientDetails(models.Model):
                      created_by = models.ForeignKey(User)
                      ...
                  

                  如何让 created_by 默认为当前登录的用户?

                  How do I make created_by default to the currently logged in user?

                  (我想这样做是为了可以将它隐藏在管理视图中,主要是因为当我保存一个实例时,我不想每次都填充它)

                  推荐答案

                  由于需要从 request 对象中获取当前登录的用户,因此无法在模型的 save-方法,但您可以例如覆盖模型管理员的 save_model-方法:

                  Since you need to get the currently logged in user from a request object you cannot get it in the model's save-method,but you can eg override the model admin's save_model-method:

                  class MyAdmin(admin.ModelAdmin):
                      def save_model(self, request, instance, form, change):
                          user = request.user 
                          instance = form.save(commit=False)
                          if not change or not instance.created_by:
                              instance.created_by = user
                          instance.modified_by = user
                          instance.save()
                          form.save_m2m()
                          return instance
                  

                  这篇关于使模型中的用户默认为当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django 管理员.在更改选择字段上隐藏字段 下一篇:在 Django 中使用 InlineAdmin 和 post_save 信号创建配置文件模型

                  相关文章

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

                    <tfoot id='om5DO'></tfoot>

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

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

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