Django - 如何在查询集中使用modelForm的关联模型到表单

时间:2022-04-21 02:31:01

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.

如何访问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))

2 个解决方案

#1


1  

When you declare a Model with

使用时声明模型

class MyModel(models.Model):
    class Meta:
        something = 'foo'

or a ModelForm with

或者带有的ModelForm

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.

有一个特殊的元类,在“读取”你的类定义时,除其他外,它将Meta属性设置/替换为_meta。

So in order to access the associated model in your ModelForm do:

因此,为了访问ModelForm中的关联模型,请执行以下操作:

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))

#2


0  

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

我发现“self.instance”有效,这正是我想要的:

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

#1


1  

When you declare a Model with

使用时声明模型

class MyModel(models.Model):
    class Meta:
        something = 'foo'

or a ModelForm with

或者带有的ModelForm

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.

有一个特殊的元类,在“读取”你的类定义时,除其他外,它将Meta属性设置/替换为_meta。

So in order to access the associated model in your ModelForm do:

因此,为了访问ModelForm中的关联模型,请执行以下操作:

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))

#2


0  

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

我发现“self.instance”有效,这正是我想要的:

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