使用 Django 管理站点自动填充 created_by 字段 [英] Auto-populating created_by field with Django admin site

查看:27
本文介绍了使用 Django 管理站点自动填充 created_by 字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Django 管理界面用于一个非常简单的 Web 应用程序,但我无法解决一个不应该那么难解决的问题..

I want to use the Django admin interface for a very simple web application but I can't get around a problem that should not be that hard to resolve ..

考虑以下事项:

class Contact(models.Model):
    name = models.CharField(max_length=250, blank=False)
    created_by = models.ForeignKey(User, blank=False)

我找不到自动填充 created_by 字段并使 Django 管理员知道它的方法.我见过的大多数方法都意味着重载对象的保存方法并将其传递给请求用户.它们都需要构建您的自定义视图和/或表单.

I can't find a way to auto-populate the created_by field and make the Django admin aware of it. Most of the method I've seen implies overloading the Object's save method and pass it the request user. They all requires to build your custom views and/or forms.

最好在管理站点中创建新联系人的表单不应该显示 created_by 字段(这很容易)并使用当前用户自动填充它(这似乎比它应该的更难).

Optimally the form to create new contacts in the admin site should not show the created_by field (which is quite easy) and auto-populate it with the current user (which seems harder than it should).

推荐答案

http://code.djangoproject.com/wiki/CookBookNewformsAdminAndUser

涉及在 ModelAdmin 对象上实现保存方法.

Involves implementing save methods on your ModelAdmin objects.

这篇关于使用 Django 管理站点自动填充 created_by 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆