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

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

问题描述

我想创建一个链接,打开一个模型的django admin添加页面,其中包含一些预填充的字段。

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为{%url'admin:myapp_mymodel_add'%},但这只是要显式)。

(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):

此方法接收请求,并将该请求转换为将以更改列表中的表单的初始参数(即,管理中的添加或编辑页面)。正如您所期望的,此方法的默认实现是使用GET参数并转换为字典;如果您对您认为是datetimes的字段执行一些额外处理,那么

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)
        }

ie,不要注意请求数据,只需手动填充初始数据。由于它是表单的初始数据,对象上的任何现有数据都将覆盖 initial 中提供的值,并且将使用初始数据,而不需要用户提供请求中的任何 GET 参数。

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参数的初始datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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