6

假设我处于通常情况下,many2many 关系中有额外的字段:

class Person(models.Model):
    name = models.CharField(max_length=128)

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

# other models which are unrelated to the ones above..

class Trip(models.Model):
  placeVisited  = models.ForeignKey(Place)
  visitor  = models.ForeignKey(Person)
 pleasuretrip = models.Boolean()

class Place(models.Model):
  name = models.CharField(max_length=128)

我想在通过 Inline 显示的 Membership 表单中添加一些额外的字段。这些字段基本上是另一个模型(Trip)实例化的捷径。Trip 可以有自己的管理视图,但需要这些快捷方式,因为当我的项目合作伙伴在系统中输入“会员”数据时,他们碰巧也有方便的“旅行”信息(也因为会员中的一些信息可以复制到 Trip 等)。

所以我想要的只是 Membership Inline 中的两个额外字段——placeVisited 和 joytrip——与 Person 实例一起,我可以在后台实例化 Trip 模型......

我发现我可以通过定义自己的表单轻松地将额外的字段添加到内联视图中。但是一旦输入了数据,如何以及何时引用它们以执行我需要做的保存操作?

class MyForm(forms.ModelForm):
 place = forms.ModelChoiceField(required=False, queryset=Place.objects.all(), label="place",)
 pleasuretrip = forms.BooleanField(required=False, label="...")

class MembershipInline(admin.TabularInline):
 model = Membership
 form = MyForm
    def save_model(self, request, obj, form, change):
        place = form.place
        pleasuretrip = form.pleasuretrip
        person = form.person
        ....
        # now I can create Trip instances with those data
        ....
        obj.save()

class GroupAdmin(admin.ModelAdmin):
 model = Group
 ....
 inlines = (MembershipInline,)

这似乎不起作用......我对save_formset方法也有点困惑......也许是我应该使用的那个?非常感谢您的帮助!!!!

4

2 回答 2

14

正如 syn 在他的回答中指出的那样,对于 TabularInline 和 StackedInline,您必须覆盖包含内联的 ModelAdmin 内的 save_formset 方法。

GroupAdmin(admin.ModelAdmin):
    model = Group
    ....
    inlines = (MembershipInline,)

    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)

        for instance in instances:
            if isinstance(instance, Member): #Check if it is the correct type of inline
                if(not instance.author):
                    instance.author = request.user
                else:
                    instance.modified_by = request.user            
                instance.save()
于 2010-08-25T18:24:40.010 回答
2

我刚刚遇到了同样的问题,似乎解决方案是对于带有内联的 save_formset 和 save_model ,它们都没有被调用。相反,您必须在被称为父级的表单集中实现它,例如

model = Group
    ....
    inlines = (MembershipInline,)

    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)

        for instance in instances:
            # Here an instance is a MembershipInline formset NOT a group...
            instance.someunsetfield = something

            # I think you are creating new objects, so you could do it here.
            instance.save()
于 2010-03-25T10:23:15.453 回答