Model.ManyToManyField.all() gives AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'

Model.ManyToManyField.all() gives AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'

我正在使用 Django 2.0.2、Python 3.6.4 和 PyCharm 2017.3.3

型号:(models.py)

class Position(models.Model):
    title = models.CharField(max_length=50)
    gang = models.ForeignKey(Gang, on_delete=models.CASCADE)
    description = models.TextField(max_length=20000)

    def __str__(self):
        return str(self.title) + ', ' + str(self.gang)

class Application(models.Model):
    positions = models.ManyToManyField(Position)
    applicant = models.ForeignKey(User, on_delete=models.CASCADE)

class Ranking(models.Model):
    position = models.ForeignKey(Position, on_delete=models.CASCADE)
    applicant = models.ForeignKey(User, on_delete=models.CASCADE)
    rank = models.IntegerField(default=3,validators=[
            MaxValueValidator(3),
            MinValueValidator(1)
        ])

形式:(在forms.py)

class RankingForm(forms.ModelForm):
    rank = forms.IntegerField(max_value=3, min_value=1)
    position = forms.ModelMultipleChoiceField(queryset=Application.positions.all())

    class Meta:
        model = Ranking
        exclude = ['applicant']
        fields = ('rank', 'position')

    def __init__(self, *args, **kwargs):
        super(RankingForm, self).__init__(*args, **kwargs)
        self.fields['rank'].widget.attrs.update({'class': 'form-control'})

我不断从

获取 RankingForm 中的 AttributeError

"position = forms.ModelMultipleChoiceField(queryset=Application.positions.all())"

当我写作时

class Application(models.Model):
    ... 

    def __str__(self):
        return str(self.positions.all())

它在 django-admin 中显示为一个 QuerySet(适用于 forms.ModelMultipleChoiceField()),但是写

    class Application(models.Model):
    ... 

    def __str__(self):
        return str(Application.positions.all())

给我同样的错误:'ManyToManyDescriptor' object has no attribute 'all'

写作

    class RankingForm(forms.ModelForm):
        ...
        position = forms.ModelMultipleChoiceField(queryset=Position.objects.all())

有效,但这不是我希望该字段显示的内容。

我想用来自特定应用程序的所有位置创建一个 ModelMultipleChoiceField(),但这个错误一直阻碍着我。似乎只引用模型是行不通的,但是引用自己呢??任何帮助是极大的赞赏! :)

顺便说一句,我还没有找到关于这个问题的任何好的文档,但是 this 似乎是 related_descriptors.py 的代码,其中 ManyToManyDescriptor 位于

您可以使用 instance 属性访问 ModelForm 对象正在使用的模型的当前实例。然后您可以使用它在 __init__:

中创建正确的查询集
class RankingForm(forms.ModelForm):
    ...

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['rank'].widget.attrs.update({'class': 'form-control'})
        self.fields['position'].queryset = self.instance.positions.all()

评估关系是使用 class.

的初始化实例的实例完成的

应用程序的一个实例。

application = Application.objects.first()
application.positions.all()

初始化后更改表单查询集。

class RankingForm(forms.ModelForm):
    rank = forms.IntegerField(max_value=3, min_value=1)
    position = forms.ModelMultipleChoiceField(queryset=Positions.objects.none())

    class Meta:
        model = Ranking
        exclude = ['applicant']
        fields = ['rank', 'position']

    def __init__(self, *args, **kwargs):
        super(RankingForm, self).__init__(*args, **kwargs)
        self.fields['rank'].widget.attrs.update({'class': 'form-control'})  
        self.fields['position'].queryset = self.instance.positions.all()