Reputation: 283
How do I access the model object associated in a ModelForm? Something like self.Meta.model? My model and form are defined as below, where I am trying to accessing the model's attribute by "self.Meta.model.attribute", but this doesn't work.
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))
Upvotes: 1
Views: 2167
Reputation: 283
I found that "self.instance" works, which is exactly what I want:
self.fields['attribute'].queryset = self.instance.attribute.all()
Upvotes: 0
Reputation: 7450
When you declare a Model with
class MyModel(models.Model):
class Meta:
something = 'foo'
or a ModelForm with
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
there is a special metaclass that upon "reading" your class definition, among
other things, it sets/replaces the Meta
attribute as _meta
.
So in order to access the associated model in your ModelForm do:
AttributeIndexForm._meta.model
But in your particular situation where you just want to customize the queryset of
the attribute
field you should do:
class AttributeIndexForm(forms.ModelForm):
class Meta:
model = AttributeIndex
def __init__(self, *args, **kwargs):
super(AttributeIndexForm, self).__init__(*args, **kwargs)
self.fields['attribute'].queryset = Attribute.objects.filter(..condition..)
self.fields['attribute'].widget = widgets.FilteredSelectMultiple("Attributes", is_stacked=False))
Upvotes: 2