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

查看:259
本文介绍了使用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管理员知道它。我看到的大部分方法意味着重载Object的save方法并传递请求用户。他们都需要构建您的自定义视图和/或表单。

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天全站免登陆