从内嵌表单保存自定义数据(django admin)

时间:2015-08-26 18:25:42

标签: python django django-forms inline-formset

我正在使用django-nested-inline包含两个级别。

class OrdersAdmin(NestedModelAdmin):
    inlines = [PointsInLine,]
    form = OrdersModelForm

class PointsInLine(NestedStackedInline):
    model = Points
    fk_name = 'order_id'
    inlines = [AddressesInLine]

class AddressesInLine(NestedStackedInline):
    model = Addresses
    fk_name = 'point'
    form = AddressModelForm

如您所见,我还为最后一级(AddressesInLine)创建了一个自定义表单,如下所示:

class AddressModelForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(forms.ModelForm, self).__init__(*args, **kwargs)
        self.fields['address'].widget = BoxSearchWidget()

    class Meta:
        fields = '__all__'
        model = Addresses

同样,我正在压倒这个领域'地址'使用自定义小部件(由于简单性,我不会包括它)。

最后,问题是:当我保存时,此自定义字段地址未保存,即使我尝试覆盖表单的save()方法,我也看不到任何字典中的值。那么,我是否在创建自定义表单时遗漏了一些内容?

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题是您正在调用car_number_plate = "ABC123" speed_in_miles_per_hour = 33 with open("file.txt", "w") as myfile: myfile.write("{0} went at {1} mph \n".format(car_number_plate, speed_in_miles_per_hour)) 方法,这是super(ModelForm, self).__init__类的__init__方法。因此,您的课程构建为普通Form,而不是Form

您应该拨打ModelForm。请记住:super(AddressModelForm, self).__init__的第一个参数是您需要获取父类型方法的子类型。您可以找到有关super here

的更多信息