如何用模型对象数据填写表格? [英] How can I fill up form with model object data?
本文介绍了如何用模型对象数据填写表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用模型实例中的数据填写表格.但是我的表单域比模型域少.如果我有这样的代码:
I want to fill up form with data from model instance. But my form has less fields than model. If I have code like this:
class Item(models.Model)
name = models.CharField(max_length=100)
price = models.PositiveIntegerField()
class ItemForm(forms.Form):
name = forms.CharField()
此功能有什么问题,看起来应该如何?
What wrong is with this function and how it should look to be good?
def bound_form(request, id):
item = Item.objects.get(id=id)
form = ItemForm(item.name)
return render_to_response('bounded_form.html', {'form': form})
我收到这样的错误:AttributeError: 'ItemForm' object has no attribute 'get'
推荐答案
def bound_form(request, id):
item = Item.objects.get(id=id)
form = ItemForm(initial={'name': item.name})
return render_to_response('bounded_form.html', {'form': form})
这篇关于如何用模型对象数据填写表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文