I am using Django to write an app which is going to have an interface for automated requests and the like, which will not use a graphical/HTML UI. As of right now, I'm using a ModelForm
to allow requests such as
我正在使用Django编写一个应用程序,它将有一个用于自动请求等的接口,它不会使用图形/HTML UI。目前,我正在使用一个ModelForm来允许诸如此类的请求
POST /addFoo?a=1&b=Hello
which will create a new Foo
with those fields and values and add it to the database. The view function looks like
它将使用这些字段和值创建一个新的Foo,并将其添加到数据库中。视图函数看起来是这样的
def addFoo(request):
form = FooForm(request)
if form.is_valid():
foo = form.save()
return succeed(id=foo.id)
else:
return fail(code=0, error=form.errors)
where succeed
and fail
are functions to generate proper responses/log entries. This part works fine. However, I would like to write a similar function editFoo
so that if there is an object with ID 1, then
成功和失败是生成正确响应/日志条目的函数。这部分工作正常。但是,我想编写一个类似的函数editFoo,以便如果有一个ID为1的对象。
POST /editFoo?id=1&a=42
will change that object's a
field to 42. The view function as I currently have it is
将物体的a场变为42。视图功能就像我现在所看到的那样。
def editFoo(request):
try:
foo = models.Foo.objects.get(id=request['id'])
except:
return fail(0, error=u'No such object exists!')
form = FooForm(request, instance=foo)
if form.is_valid():
form.save()
return succeed()
else:
return fail(code=1, error=form.errors)
However, when I test this form.errors contains error messages to the effect that the unspecified fields are required. Since the initiation of these requests will be by the client (that is, I cannot pre-load an HTML <form>
for them to edit), I can't seem to find a way to make this sort of view work.
但是,当我测试这个表单时。错误包含错误消息,其效果是需要未指定的字段。由于这些请求的启动将由客户端(也就是说,我不能预先加载一个HTML
Thanks in advance for your advice.
谢谢你的建议。
EDIT: Model and Form are really simple:
编辑:模型和表单非常简单:
class Foo(Model):
a = IntegerField()
b = CharField(max_length=23)
and
和
class FooForm(ModelForm):
class Meta:
model = Foo
1 个解决方案
#1
1
You need to use a form class that contains only the fields that you're expecting to get from your POST, or that allows the fields to be omitted even though they are required on the model itself.
您需要使用一个表单类,它只包含希望从POST获得的字段,或者允许忽略字段,即使模型本身需要字段。
Limiting the fields present:
限制字段:
class FooEditForm(ModelForm):
class Meta:
model = Foo
fields = ('a',)
Allowing the fields to be omitted:
允许省略字段:
class FooEditForm(ModelForm):
class Meta:
model = Foo
a = forms.IntegerField(required=False)
b = forms.CharField(required=False, max_length=23)
Or edit them in the form's __init__
method:
或者用__init__方法编辑它们:
class FooEditForm(ModelForm):
class Meta:
model = Foo
def __init__(self, *args, **kwargs):
super(FooEditForm, self).__init__(*args, **kwargs)
for field in self.fields.itervalues():
field.required = False
#1
1
You need to use a form class that contains only the fields that you're expecting to get from your POST, or that allows the fields to be omitted even though they are required on the model itself.
您需要使用一个表单类,它只包含希望从POST获得的字段,或者允许忽略字段,即使模型本身需要字段。
Limiting the fields present:
限制字段:
class FooEditForm(ModelForm):
class Meta:
model = Foo
fields = ('a',)
Allowing the fields to be omitted:
允许省略字段:
class FooEditForm(ModelForm):
class Meta:
model = Foo
a = forms.IntegerField(required=False)
b = forms.CharField(required=False, max_length=23)
Or edit them in the form's __init__
method:
或者用__init__方法编辑它们:
class FooEditForm(ModelForm):
class Meta:
model = Foo
def __init__(self, *args, **kwargs):
super(FooEditForm, self).__init__(*args, **kwargs)
for field in self.fields.itervalues():
field.required = False