如何访问 ModelForm 中关联的模型对象?像 self.Meta.model 这样的东西?我的模型和表单定义如下,我试图通过“self.Meta.model.attribute”访问模型的属性,但这不起作用。
class Attribute(models.Model):
name = models.CharField(max_length=64)
class AttributeIndex(models.Model):
product = models.OneToOneField(Product)
attribute = models.ManyToManyField(Attribute)
class AttributeIndexForm(forms.ModelForm):
class Meta:
model = AttributeIndex
def __init__(self, *args, **kwargs):
super(AttributeIndexForm, self).__init__(*args, **kwargs)
self.fields['attribute'] = forms.ModelMultipleChoiceField(queryset=self.Meta.model.attribute.all, widget=widgets.FilteredSelectMultiple("Attributes", is_stacked=False))