在一个内嵌的形式额外的工作领域 - save_model,save_formset,不能赚取差价的

2019-08-18 14:27发布

假如我是在通常情况下那里是在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)

我想在得到通过内联显示的会员形式加入一些额外的字段 。 这些领域基本上都是一个快捷方式到另一种模式(旅行)的实例。 旅行可以有自己的管理意见,但需要这些快捷方式,因为当我的项目合作伙伴在系统中输入“会员”的数据,他们正好也有“跳闸”信息方便(还因为一些成员的信息都可以只被复制至行程等等)。

因此,所有我想有在籍内嵌了两个附加字段- placeVisited和pleasuretrip -这与Person实例在一起会让我实例在后台旅行模式 ...

我发现我可以很容易地通过定义自己的表单中添加额外的字段的内嵌视图。 但是,一旦数据已经输入,如何以及何时参考,以便他们执行我需要做保存操作?

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方法不解......也许是一个我应该使用? 提前的帮助非常感谢 !!!!

Answer 1:

正如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()


Answer 2:

我正好碰到同样的问题,似乎解决的办法是,对于这两种save_formset和save_model与内联,他们不叫。 相反,你必须以一种被称为是父如该formset实现它

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()


文章来源: Working with extra fields in an Inline form - save_model, save_formset, can't make sense of the difference