如何保存 django 内联表单集的顺序?

发布于 2024-11-25 13:50:24 字数 332 浏览 0 评论 0原文

在 Django 中,我在设置了 order_with_respect_to 的模型上使用带有 can_order = True 的内联表单集。我已经在前端设置了拖放操作,这会导致 Django 自动生成的 ORDER 表单字段(我已隐藏)发生变化以反映新订单。我已经验证我已将新订单正确发布到服务器,但 Django 似乎忽略它,并按原始顺序保存模型。数据库中自动创建的 _order 字段永远不会改变。

如何让 Django 使用表单集中指定的顺序保存模型?除了在表单集上调用 save() 之外,我还需要做任何特殊的事情吗?

In Django, I'm using an inlineformset with can_order = True, on a model that has order_with_respect_to set. I've set up drag and drop on the front end, which results in Django's autogenerated ORDER form fields (which I've hidden) changing to reflect the new order. I've verified I'm POSTing the new order correctly to the server, but Django seems to ignore it, and saves the models in the original order. The automatically-created _order fields in the database never change.

How can I get Django to save the models using order specified in the formset? Do I need to do anything special other than calling save() on the formset?

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

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

发布评论

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

评论(2

无言温柔 2024-12-02 13:50:24

Evan Borgstrom 提出的解决方案不能与 can_delete=True 一起使用。

这是我的解决方案,也适用于 can_delete=True

for instance in formset.save(commit=False):
    # ... do something with m2m relationships ...
for form in formset.ordered_forms:
    form.instance.order = form.cleaned_data['ORDER']
    form.instance.save()

Evan Borgstrom proposed solution does not work together with can_delete=True.

Here is my solution that also works with can_delete=True:

for instance in formset.save(commit=False):
    # ... do something with m2m relationships ...
for form in formset.ordered_forms:
    form.instance.order = form.cleaned_data['ORDER']
    form.instance.save()
静若繁花 2024-12-02 13:50:24

我遇到了同样的问题,在深入研究 Django 源代码后发现这是你需要自己做的事情。

我最初的实现看起来像这样:

services = service_formset.save(commit=False)
for service in services:
    # ... do something with m2m relationships ...
    service.save()

但是,正如您所发现的,这并没有设置顺序。因此,我的实现现在看起来像:

for form in service_formset:
    service = form.save(commit=False)
    # ... do something with m2m relationships ...
    service.order_order = form.cleaned_data['ORDER']
    service.save()

在上面的示例中,“order_order”是我用来跟踪模型上的订单的字段。

另请记住,您需要指定模型的 Meta 类的“ordering”属性,以确保再次生成表单集时元素的顺序正确。

class Service(models.Model):
    # ...
    order_order = models.IntegerField(default=0)
    class Meta:
        ordering = ['order_order',]

I had the same problem and after digging through the Django source figured that it's something you need to do on your own.

My original implementation looked something like this:

services = service_formset.save(commit=False)
for service in services:
    # ... do something with m2m relationships ...
    service.save()

However, as you've found this doesn't set the ORDER. So instead my implementation now looks like:

for form in service_formset:
    service = form.save(commit=False)
    # ... do something with m2m relationships ...
    service.order_order = form.cleaned_data['ORDER']
    service.save()

In my example above 'order_order' is the field I'm using to track order on my models.

Also remember that you need to specify the 'ordering' attribute of your model's Meta class to ensure that when you generate the formset again the elements are in the correct order.

class Service(models.Model):
    # ...
    order_order = models.IntegerField(default=0)
    class Meta:
        ordering = ['order_order',]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文