我正在 Django 管理员中创建一个内联表单,该表单假定具有条件值。
例如,我有这个模型
class Category (models.Model):
name = models.CharField(max_length=500)
parent = models.ForeignKey('self', blank=True, null=True, related_name='subcategory')
def __unicode__(self):
return self.name
class Business(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=500)
def __unicode__(self):
return self.name
class DescriptionType (models.Model):
category = models.ManyToManyField(Category)
name = models.CharField(max_length=500)
def __unicode__(self):
return self.name
class Descriptions (models.Model):
descriptiontype = models.ForeignKey(DescriptionType)
business = models.ForeignKey(Business)
所以,我有一个Category
(“餐厅”),并且DescriptionType
("Food Speciality",它们只属于 "Restaurants")。
所以我想用“餐厅”类别创建业务“Foster Hollywood”,我想在管理内联 DescriptionsInline 中只允许选择属于“餐厅”类别的 DescriptionTypes。我当前的解决方案显示了描述类型的所有值
class DescriptionsInline(admin.TabularInline):
model = Descriptions
extra = 0
class BusinessAdmin(admin.ModelAdmin):
inlines = [DescriptionsInline]
list_display = ('name',)
search_fields = ['name']
admin.site.register(Business, BusinessAdmin)
我怎样才能使内联DescriptionsInline
显示仅DescriptionTypes
来自所选类别的Business
?