English 中文(简体)
在Django以编辑形式(数据预人口)通过整个一整个原生对象(数据预人口)
原标题:Pass an entire inital object in an edit form (data pre-population) in Django

我有一个非常复杂的窗体,我选择不使用 ModelForm ,因为我需要灵活性和对字段的控制。由于我没有使用 ModelForm ,我不能简单地做一些类似 Instance=ord 的事情,那里有 order =order.objects.get(pk=1)

目前,我正在预先将所有在 forms.py forms.py 中的 初始views.py 类似这样的 栏相对的字段都填充为人口。

self.fields[ work_type ] = forms.ChoiceField(choices=Order.WORK_TYPE_CHOICES, initial=order.work_type)

但我想知道,我是否可以将整个 < code> order 反对表单,或者我是否必须宣布 < code> first 到 every 字段?

是否有一种方法 做一些类似

order_form = OrderEditForm(data=request.POST, initial=order)

views. py 中?

最佳回答

今后关于其他人:

我读完SO的评语和回答后发现,即使你最终以人工手动的方式(使用诸如self.fields[fo]=窗体.Char Form () 之类的东西)明确定义每个字段,也最好使用ModelForm

无论如何,如果试图以某种形式通过当前值的字典,那么将模型转换为字典的最佳(内建)方式实际上是使用model_to_dict :

from django.forms.models import model_to_dict

order = Order.objects.get(pk=1)
dictionary = model_to_dict(order)
form = OrderEditForm(dictionary)

我从"http://www.ironzebra.com/news/3/how-to-qaid-ready-a-django-edit-form- from-object-intance" rel=“nofollow”这个博客 得到了解答。 我希望这对某人有帮助。

问题回答

I have a very complicated form and I choose to not use ModelForm since I needed flexibility and control over the fields

使用窗体,您可以在模型格式中做什么都可以做,比如在字段中添加新字段或超称属性等。

But I was wondering if I could pass the entire order object to a form or do I have to declare initial to every field?

您可以将顺序对象传送到窗体中,但您仍须在窗体或视图函数中分别填充每个字段。

所以,在你看来,你会做这样的事情:

intial = { order_number : order.number,  order_id : order.id}

form = OrderForm(initial=initial)

将数据预先传播到窗体的最简单方式是将字典作为第一个参数,通过字典来解构构建器。

order_form = OrderEditForm(order.__dict__())

此处 是传递“顺序”对象属性给字典的一种方法, 字典中每个属性的名称都作为密钥, 其内容则作为值 。

例如如何“发明”这种方法,

order_initial = Order.objects.filter(pk=order.pk).values()[0]

然后以下列方式构造窗体:

order_form = OrderEditForm(order_initial)

看看"https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view" rel=“nofollow”>这个 的例子(它们如何在“日记”时间填充值):





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签