django 管理员“添加页面"来自 GET 参数的初始日期时间 [英] django admin "add page" initial datetime from GET parameters

查看:11
本文介绍了django 管理员“添加页面"来自 GET 参数的初始日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个链接,该链接可以打开模型的 django 管理员添加页面,其中一些字段已预先填写.

I want to create a link which opens the django admin add page of a model with some fields pre filled.

我检查了是否可以将参数添加到添加表单的GET字典中,如:

I checked that it is possible to add parameters to the GET dictionary of the add form, as in:

<a href='/admin/myapp/mymodel/add?name=John'>add new mymodel with name John</a>

(实际上我用 {% url 'admin:myapp_mymodel_add' %} 得到了 url,但这只是为了明确).

(actually I get the url with {% url 'admin:myapp_mymodel_add' %} but this is just to be explicit).

现在这适用于数值、文本值和外键.但是 DateTime 字段呢?当我尝试传递这样一个字段时(我尝试了许多格式,例如2014-05-09 10:10:00"),我总是收到服务器错误"的抱怨:

Now this works fine with numerical values, text values and foreign keys. But what about DateTime fields? When I try to pass such a field (I tried many formats like for example "2014-05-09 10:10:00") I always get a "server error" complaining that:

'unicode' 对象没有属性'date'

'unicode' object has no attribute 'date'

在代码行中:

django/forms/widgets.py 在解压中,第 902 行

django/forms/widgets.py in decompress, line 902

 def decompress(self, value):
        if value:
            value = to_current_timezone(value)
            return [value.date(), value.time().replace(microsecond=0)] ...
        return [None, None]

变量value"的值是我在 URL 上传递的值...

where the variable "value" has the value I'm passing on the URL...

问题 1. 我认为服务器代码不应该根据用户传递的值引发异常...这不是 django 库中的错误吗?

Question 1. I would assume that server code should not raise an exception depending on values passed by the user... isn't this a bug in the django libraries?

问题 2.如何解决我的问题,即通过 GET 参数将初始日期时间传递到模型的管理添加"页面?

Question 2. How can I solve my problem i.e. pass an initial datetime through GET parameters to the admin "add" page of a model?

推荐答案

不,这不是错误;可能有人认为可以改进默认行为,但现有的实现是合理的第一遍(正如它适用于整数、字符串、外键等的事实所证明的那样).有足够多的关于模型的元数据和足够的反序列化代码,可能可以在此处进行稍微更好的处理,但我不认为这是一个直接的错误.

No, it isn't a bug; there's maybe an argument that the default behaviour could be improved, but the existing implementation is a reasonable first pass (as evidenced by the fact that it works on integers, strings, foreign keys, etc). There's enough metadata about the model, and enough deserialization code that it would be possible to do slightly better handling here, but I don't think it's a straight up bug.

好消息是有官方的方法来处理这个问题;在您的 ModelAdmin 类上,定义:

The good news is that there is an official way to handle this; on your ModelAdmin class, define:

def get_changeform_initial_data(self, request):

此方法接受请求,并将该请求转换为将作为 initial 参数传递给更改列表(即管理中的添加或编辑页面)上的表单的值.如您所料,此方法的默认实现是获取 GET 参数并转换为字典";如果你对你知道是日期时间的字段做一些额外的处理,那么

This method takes the request, and converts that request into the value that will be passed in as the initial argument to the form on a change list (i.e., an add or edit page in Admin). As you might expect, the default implementation of this method is "take the GET arguments and convert into a dictionary"; if you do some additional handling for the fields that you know to be datetimes, then

或者,您根本不必使用请求对象.如果您知道Person"对象的初始名称始终为Unknown",初始开始日期为 2014 年 1 月 1 日,那么您只需编写:

Alternatively, you don't have to use the request object at all. If you know that "Person" objects will always have an initial name of "Unknown", and an initial start date of 1 Jan 2014, then you can just write:

class PersonAdmin(ModelAdmin):
    ...
    def get_changeform_initial_data(self, request):
        return {
            'name': 'Unknown',
            'start_date': date(2014, 1, 1)
        }

即,不要关注请求数据,只需手动填充初始数据.由于它是表单的初始数据,因此对象上的任何现有数据都将覆盖 initial 中提供的值,并且将使用该初始数据而无需用户提供 anyGET 请求中的参数.

i.e., don't pay any attention to the request data, just populate the initial data manually. Since it's initial data to the form, any existing data on the object will override the value provided in initial, and that initial data will be used without the user needing to provide any GET arguments in their request.

这篇关于django 管理员“添加页面"来自 GET 参数的初始日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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