如何用模型对象数据填写表单?

时间:2022-06-04 06:38:27

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

I getting error like this: AttributeError: 'ItemForm' object has no attribute 'get'

我得到这样的错误:AttributeError:'ItemForm'对象没有属性'get'

2 个解决方案

#1


26  

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

#2


43  

Generally when creating a form for a Model, you will want to use ModelForm. It keeps to the DRY principle such that you do not have to redefine field types for the form class. It also automatically handles validation. You retain full flexibility to customize the fields and widgets used. Use fields to specify the fields you want or exclude to specify fields to ignore. With your example:

通常,在为模型创建表单时,您将需要使用ModelForm。它遵循DRY原则,因此您不必重新定义表单类的字段类型。它还会自动处理验证。您可以保留足够的灵活性来自定义使用的字段和小部件。使用字段指定要或要排除的字段以指定要忽略的字段。用你的例子:

from django import forms
from django.shortcuts import get_object_or_404

class ItemForm(forms.ModelForm):
    class Meta:
        model = Item
        fields = ("name", )

def bound_form(request, id):
    item = get_object_or_404(Item, id=id)
    form = ItemForm(instance=item)
    return render_to_response('bounded_form.html', {'form': form})

get_object_or_404() is useful here as a form of error handling. Using Item.objects.get(id=id) on a missing ID will throw an uncaught Item.DoesNotExist exception otherwise. You could use a try/except block also of course.

get_object_or_404()在这里作为错误处理的一种形式很有用。在缺少的ID上使用Item.objects.get(id = id)将抛出未捕获的Item.DoesNotExist异常。当然,你也可以使用try / except块。

#1


26  

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

#2


43  

Generally when creating a form for a Model, you will want to use ModelForm. It keeps to the DRY principle such that you do not have to redefine field types for the form class. It also automatically handles validation. You retain full flexibility to customize the fields and widgets used. Use fields to specify the fields you want or exclude to specify fields to ignore. With your example:

通常,在为模型创建表单时,您将需要使用ModelForm。它遵循DRY原则,因此您不必重新定义表单类的字段类型。它还会自动处理验证。您可以保留足够的灵活性来自定义使用的字段和小部件。使用字段指定要或要排除的字段以指定要忽略的字段。用你的例子:

from django import forms
from django.shortcuts import get_object_or_404

class ItemForm(forms.ModelForm):
    class Meta:
        model = Item
        fields = ("name", )

def bound_form(request, id):
    item = get_object_or_404(Item, id=id)
    form = ItemForm(instance=item)
    return render_to_response('bounded_form.html', {'form': form})

get_object_or_404() is useful here as a form of error handling. Using Item.objects.get(id=id) on a missing ID will throw an uncaught Item.DoesNotExist exception otherwise. You could use a try/except block also of course.

get_object_or_404()在这里作为错误处理的一种形式很有用。在缺少的ID上使用Item.objects.get(id = id)将抛出未捕获的Item.DoesNotExist异常。当然,你也可以使用try / except块。