我有4节课:
class Material(models.Model):
name = models.CharField(_('name'), max_length=50)
class Property(models.Model):
name = models.CharField(_('name'), max_length=50)
class Physic(models.Model):
name = models.ForeignKey(Property, verbose_name=_('name'), null=True, blank=True,)
lapropriete = models.CharField(_('property'), max_length=100)
class UniteProperty2(models.Model):
name = models.ForeignKey(Material, verbose_name=_('name'))
nature_unit = models.ForeignKey(Property, verbose_name=_('category'))
choix = models.ForeignKey(Physic, verbose_name=_('properties'), null=True, blank=True, related_name='choix')
我想要做的是当我在UniteProperty2中选择nature_unit时,它只显示属性(在管理界面中) 属于当前类别&#39; (例如:如果我选择Mechanical,我会在下拉列表中选择&#39; Hardness vickers&#39;年轻模数&#39;年轻模数&#39; choix&#39;)< / p> 在admin.py文件中
,我有
class UniteProperty2InlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UniteProperty2InlineForm, self).__init__(*args, **kwargs)
self.fields['choix'].queryset = Physic.objects.filter(
name=self.instance.name)
class UniteProperty2Inline(admin.TabularInline):
model = UniteProperty2
form = UniteProperty2InlineForm
class MaterialAdmin(admin.ModelAdmin):
inlines = (UniteProperty2Inline, ..)
但它不起作用......我有错误&#39;没有例外提供&#39;例外类型:DoesNotExist
问题是:name__name = self.instance.name
答案 0 :(得分:0)
self.instance.name
- 以ur形式引用UniteProperty2名称 - 这是一个Material obj
所以试试这个
self.fields['choix'].queryset = Physic.objects.filter(
name=self.instance.choix )
或所有选项: self.fields [&#39; choix&#39;]。queryset = Physic.objects.filter( name = self.instance.choix)