使用Inline表单中的额外字段(save_model,save_formset)无法理解差异 [英] Working with extra fields in an Inline form - save_model, save_formset, can't make sense of the difference

查看:608
本文介绍了使用Inline表单中的额外字段(save_model,save_formset)无法理解差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose I am in the usual situation where there're extra fields in the many2many relationship:

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 显示的船形式。这些字段基本上是另一个模型(Trip)的实例化的快捷方式。旅行可以有自己的管理员视图,但是这些快捷方式是必需的,因为当我的项目合作伙伴在系统中输入会员资格数据时,他们碰巧也有旅行信息方便(同时也因为会员资格中的一些信息可以复制到旅行等)。

I want to add some extra fields in the Membership form that gets displayed through the Inline. These fields basically are a shortcut to the instantiation of another model (Trip). Trip can have its own admin views, but these shortcuts are needed because when my project partners are entering 'Membership' data in the system they happen to have also the 'Trip' information handy (and also because some of the info in Membership can just be copied over to Trip etc. etc.).

所有我想要的是会员内联中的两个额外字段 - placeVisited和pleasuretrip - 与Person实例一起将让我实例化旅行模型在后台 ...

So all I want to have is two extra fields in the Membership Inline - placeVisited and pleasuretrip - which together with the Person instance will let me instantiate the Trip model in the background...

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

I found out I can easily add extra fields to the inline view by defining my own form. But once the data have been entered, how and when to reference to them in order to perform the save operations I need to do?

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 方法...可能是我应该使用的那个吗? 许多谢谢提前为帮助!!!!

This doesn't seem to work... I'm also a bit puzzled by the save_formset method... maybe is that the one I should be using? Many thanks in advance for the help!!!!

推荐答案

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

As syn points out in his answer, for TabularInline and StackedInline, you have to override the save_formset method inside the ModelAdmin that contains the inlines.

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

这篇关于使用Inline表单中的额外字段(save_model,save_formset)无法理解差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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