我正在使用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()方法,我也看不到任何字典中的值。那么,我是否在创建自定义表单时遗漏了一些内容?
提前致谢!
答案 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。