我有两个模型(ModelParent和ModelChild)在主题模型上具有相同的m2m字段。 ModelChild在ModelParent上有一个外键,而ModelChild在管理页上被定义为ModelParent的内联。在
### models.py ###
class Subject(Models.Model):
pass
class ModelParent(models.Model):
subjects_parent = ManyToManyField(Subject)
class ModelChild(models.Model):
parent = ForeignKey(ModelParent)
subjects_child = ManyToManyField(Subject)
### admin.py ###
class ModelChildInline(admin.TabularInline):
model = ModelChild
class ModelParentAdmin(admin.ModelAdmin):
inlines = [ModelChildInline]
admin.site.register(ModelParent, ModelParentAdmin)
不过,我有一个重要的限制,ModelChild的subjects\u child字段不能引用subject\u parent对其subjects\u parent所做的任何主题。在
所以,如果我在管理页面上为两个模型选择相同的主题(在Subject_parent和Subject_child中),我如何验证这一点? 如果只有一个字段发生更改,您将根据数据库对其进行验证,但是如果两个字段都发生了更改(subject\u parent和subject_child)? 如何在保存前同时验证这两个表单?在
我从继承了一个名为modeldAdminWithInline的新类管理模型管理员而修改后的方法add_view(…)和change_view(…)调用函数is_cross_valid(self,form,formsets),可以一起验证所有表单。 两种功能都有:
更改为:
^{pr2}$新函数is_cross_valid(…)的定义如下:
因此,如果不更改is_cross_valid(…)函数,新类的工作方式应该与ModelAdmin完全相同。在
现在我的管理员py看起来像这样:
这个解决方案有点老套,但它很管用:)。显示的错误与常规ModelForm和ModelAdmin类的错误相同。django1.2(应该很快发布)应该有模型验证,所以我希望这个问题可以得到更好的解决。在
clean()方法没有admin()类。它们的形态确实如此。每个管理类都有一个名为form的参数。只需扩展form admin()和form admin()方法即可。 示例:
相关问题 更多 >
编程相关推荐