模型上的 Django 用户字段

发布于 2024-10-20 11:06:16 字数 1231 浏览 2 评论 0原文

我希望我的应用程序的每个模型都存储创建其条目的用户。我做了什么:

class SomeModel(models.Model):
    # ...
    user = models.ForeignKey(User, editable = False)

class SomeModelAdmin(admin.ModelAdmin):
    # ...
    def save_model(self, request, obj, form, change):
        # Get user from request.user and fill the field if entry not existing

我的问题:由于有一个完整的应用程序用于用户身份验证和历史记录,是否有一种简单的方法(也许更定向或标准化)使用该应用程序的任何功能,而不是对每个模型执行上述过程?

更新:

这就是我所做的。在我看来真的很难看。如果有聪明的方法,请告诉我。

我扩展了我想要在 models.py 上拥有这些字段的所有模型:

class ManagerLog(models.Model):
    user = models.ForeignKey(User, editable = False)
    mod_date = models.DateTimeField(auto_now = True, editable = False, verbose_name = 'última modificação')
    class Meta:
        abstract = True

在 admin.py 中,我对以下类做了同样的事情:

def manager_log_save_model(self, request, obj, form, change):
    obj.user = request.user
    return obj

然后,我还需要在每个扩展模型上覆盖 save_model:

class ExtendedThing(ManagerLogAdmin):
    # ...
    def save_model(self, request, obj, form, change):
        obj = manager_log_save_model(self, request, obj, form, change)
        # ... other stuff I need to do here

I would like every model of my app to store the user that created its entries. What I did:

class SomeModel(models.Model):
    # ...
    user = models.ForeignKey(User, editable = False)

class SomeModelAdmin(admin.ModelAdmin):
    # ...
    def save_model(self, request, obj, form, change):
        # Get user from request.user and fill the field if entry not existing

My question: As there's an entire app for User authentication and history, is there an easy way (perhaps, more oriented or standardized) of using any feature of this app instead of doing the above procedure to every model?

Update:

Here's what I did. It looks really ugly to me. Please let me know if there's a clever way of doing it.

I extended all the models i wanted to have these fieds on models.py:

class ManagerLog(models.Model):
    user = models.ForeignKey(User, editable = False)
    mod_date = models.DateTimeField(auto_now = True, editable = False, verbose_name = 'última modificação')
    class Meta:
        abstract = True

In admin.py, I did the same with the following class:

def manager_log_save_model(self, request, obj, form, change):
    obj.user = request.user
    return obj

Then, I also need to to override save_model on every extended model:

class ExtendedThing(ManagerLogAdmin):
    # ...
    def save_model(self, request, obj, form, change):
        obj = manager_log_save_model(self, request, obj, form, change)
        # ... other stuff I need to do here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2024-10-27 11:06:16

更简单的方法,使用 save_model

class MyAdmin(admin.ModelAdmin):
    ...
    def save_model(self, request, obj, form, change):
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
        obj.save()

参见: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model

more easy way,use save_model

class MyAdmin(admin.ModelAdmin):
    ...
    def save_model(self, request, obj, form, change):
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
        obj.save()

see:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model

油饼 2024-10-27 11:06:16

我认为你应该查看 django 包版本控制部分。所有这些应用程序都会跟踪您的模型的更改、谁进行了这些更改以及何时进行这些更改。

I think you should check out the django packages section on versioning. All those apps will track changes to your model, who made those changes and when.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文