kli
kli

Reputation: 283

Django - How do I use the associated model of the modelForm in queryset to the form

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

Answers (2)

kli
kli

Reputation: 283

I found that "self.instance" works, which is exactly what I want:

 self.fields['attribute'].queryset = self.instance.attribute.all()

Upvotes: 0

rantanplan
rantanplan

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

Related Questions