保存“通过" M2M Django Admin中的内联 [英] Save M2M "Through" Inlines in Django Admin

查看:132
本文介绍了保存“通过" M2M Django Admin中的内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,如果存在ManyToManyField的中间通过表,则Django的ModelAdmin/ModelForm不允许您使用save_m2m().

Apparently Django's ModelAdmin/ModelForm doesn't allow you to use save_m2m() if there's an intermediate through table for a ManyToManyField.

models.py:

models.py:

from django.db import models


def make_uuid():
    import uuid
    return uuid.uuid4().hex


class MyModel(models.Model):
    id = models.CharField(default=make_uuid, max_length=32, primary_key=True)
    title = models.CharField(max_length=32)
    many = models.ManyToManyField("RelatedModel", through="RelatedToMyModel")

    def save(self, *args, **kwargs):
      if not self.id:
        self.id = make_uuid()
      super(GuidPk, self).save(*args, **kwargs)


class RelatedModel(models.Model):
    field = models.CharField(max_length=32)


class RelatedToMyModel(models.Model):
    my_model = models.ForeignKey(MyModel)
    related_model = models.ForeignKey(RelatedModel)
    additional_field = models.CharField(max_length=32)

admin.py:

from django import forms
from django.contrib import admin

from .models import MyModel


class RelatedToMyModelInline(admin.TabularInline):
    model = MyModel.many.through


class MyModelAdminForm(forms.ModelForm):
    class Meta:
        model = MyModel


class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm
    inlines = (RelatedToMyModelInline, )


admin.site.register(MyModel, MyModelAdmin)

如果我先保存MyModel,然后通过内联添加新的贯穿模型,则效果很好,但是如果我尝试设置内联同时又为新的MyModel添加数据,则会收到Django Admin错误请更正错误如下."下面没有突出显示任何内容.

If I save MyModel first and then add a new related through model via the inline it works fine, but if I try to set the inline while also adding data for a new MyModel, I get the Django Admin error "Please correct the error below." with nothing highlighted below.

如何保存MyModel,然后再保存内联中介模型?显然,一旦Django保存MyModel,就可以保存直通模型-因此,我只是在寻找一个钩子.我尝试通过在调用instance.save()之后调用save_m2m()来覆盖表单的save()方法,但是显然,这对于带有穿透表的M2M无效.

How can I have it save MyModel and then save the inline intermediary models after? Clearly Django can save the through model once it has saved MyModel - so I'm just looking for a hook into that. I tried overriding the form's save() method by calling save_m2m() after calling instance.save(), but apparently that doesn't work for M2Ms with a through table.

我正在使用Django 1.2,但这在1.3中仍然是一个问题.

I'm using Django 1.2, but this is still an issue in 1.3.

更新:好吧,我制作了一个如上所述的测试应用程序来隔离问题,并且看来它可以按预期工作,在将MyModel对象保存为...后正确保存了M2M中介对象.只要我让Django在运行python manage.py syncdb时自动创建MyModel.id字段-添加GUID id字段后,它将不再起作用.

UPDATE: Well, I made a test app like above to isolate the problem, and it appears that it works as expected, correctly saving the M2M intermediary object after saving the MyModel object... as long as I let Django automatically create the MyModel.id field when running python manage.py syncdb - once I added the GUID id field, it no longer works.

这闻起来越来越像Django的错误.

This smells more and more like a Django bug.

推荐答案

在MyModelAdmin中,您可以尝试覆盖save_formset方法.这样,您可以选择保存顺序.

In your MyModelAdmin you might try overriding the save_formset method. This way you can choose the order in which you save.

这篇关于保存“通过" M2M Django Admin中的内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆