模型表单集问题

发布于 2024-07-20 07:41:14 字数 621 浏览 3 评论 0原文

我有一个模型,需要多次显示表单。 我已经在 modelformset 下使用了它。 我似乎对该模型的 id 有问题,它也是模型的主键。
我用我想要编辑的数据预先填充表单集。
但每当我单击“提交”时,它都会刷新页面,并显示错误“(隐藏字段 ID),此 None 已存在。”
此错误专门针对隐藏的“id”字段。

<input type="hidden" id="id_form-0-id" value="2972" name="form-0-id"/>

这是模板中的片段。 (我从萤火虫那里得到的) 由于表单无效,我无法保存数据,因此可能出现什么问题。

ProfilesFormSet = modelformset_factory(Profile,exclude = ( <items spearated by commas>), extra=0) 
profile_form_set = ProfilesFormSet(queryset = Profile.objects.filter(userprofile=userprofile).order_by('-modified_on')) 

这是代码片段。

I have a model for which the need is to show the form multiple times. I have used it under a modelformset. I seem to have a problem with the id of this model which is also the primary key for the model.
I prepopulate the formset with data which I wish to edit.
But whenever I click on submit it refreshes the page back with an error saying '(Hidden field id) with this None already exists.'
This error comes specifically for the 'id' field which is hidden

<input type="hidden" id="id_form-0-id" value="2972" name="form-0-id"/>

This is the snippet from the template. (I got it from firebug)
What could the issue possibly be since the form is invalid I am not able to save the data.

ProfilesFormSet = modelformset_factory(Profile,exclude = ( <items spearated by commas>), extra=0) 
profile_form_set = ProfilesFormSet(queryset = Profile.objects.filter(userprofile=userprofile).order_by('-modified_on')) 

This is the code snippet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-07-27 07:41:14

如果您使用 PostgreSQL 和 1.1beta 之前的任何版本的 Django,并且您的模型没有定义默认排序,我认为您可能会看到与从数据库返回的对象排序不一致相关的错误(请参阅 Django Trac门票 9076975810163 等)。

尝试在模型上设置默认顺序:

class Meta:
    ordering = ('some_field',)

看看是否可以解决问题。

If you're using PostgreSQL and any version of Django prior to 1.1beta, and your model does not have a default ordering defined, I think you're probably seeing the bug related to inconsistent ordering of objects returned from the database (see Django Trac tickets 9076, 9758, 10163 among others).

Try setting a default ordering on the model:

class Meta:
    ordering = ('some_field',)

See if that fixes it.

木有鱼丸 2024-07-27 07:41:14

我相信此错误是由以下原因之一引起的:

  • 您在表单集中使用的 Django 表单对象不包含模型的主键 (id)。 但是,由于您使用的是 modelformset_factory ,因此情况不应该如此(您也不会收到该错误消息)。

  • 模板中的 HTML 表单不包含主键,即使作为隐藏字段也是如此。 确保您的模板中的 {{ for form in formset }} 循环内有 {{ form.id }} 或类似内容。

我现在想不出更多的原因,但我确信它们都与从浏览器客户端 POST 回来的表单以某种方式缺少 id 字段有关。

I believe this error is caused by one of the following:

  • The Django form object you are using inside the formset does not include the primary key (id) of the model. However, since you are using modelformset_factory this shouldn't be the case (you also wouldn't be getting that error message).

  • The HTML form in your template does not include the primary key, even as a hidden field. Make sure you have {{ form.id }} or something like that in your template, inside the {{ for form in formset }} loop.

I can't think of any more reasons at the moment, but I'm sure they are all going to be related to the form POST'ed back from the browser client is missing the id field somehow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文