Model.ManyToManyField.all()给出AttributeError:'ManyToManyDescriptor'对象没有属性'all' [英] Model.ManyToManyField.all() gives AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'

查看:368
本文介绍了Model.ManyToManyField.all()给出AttributeError:'ManyToManyDescriptor'对象没有属性'all'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Django 2.0.2, Python 3.6.4 and PyCharm 2017.3.3

模型:(在models.py中)

Models: (in 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中)

Form: (in 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'})

我一直从RankpForm中获取AttributeError

I keep getting the AttributeError in RankingForm from

位置= Forms.ModelMultipleChoiceField(queryset = Application.positions.all())"

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

我写的时候

class Application(models.Model):
    ... 

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

它在django-admin中显示为QuerySet(适用于Forms.ModelMultipleChoiceField()),但正在编写

it shows in django-admin as a QuerySet (which works for forms.ModelMultipleChoiceField()), but writing

    class Application(models.Model):
    ... 

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

给我同样的错误:'ManyToManyDescriptor'对象没有属性'all'

gives me the same error: 'ManyToManyDescriptor' object has no attribute 'all'

写作

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

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

works, but this is not what i want the field to display.

我想使用特定应用程序中的所有位置制作一个ModelMultipleChoiceField(),但是此错误一直困扰着您.似乎仅引用模型是行不通的,但引用self却可以?任何帮助是极大的赞赏! :)

I want to make a ModelMultipleChoiceField() with all the positions from a specific application, but this error keeps getting in the way. It seems that just referencing a model doesn't work, but referencing self does?? Any help is greatly appreciated! :)

顺便说一句,关于这个问题,我还没有找到任何好的文档,但是

Btw, I haven't found any good documentation on this problem, but this seems to be the code for related_descriptors.py where ManyToManyDescriptor is located

推荐答案

对关系的评估是通过作为该类的初始化实例的实例完成的.

Evaluating relationships are done with an instance that is an initialized instance of the class.

该应用程序的实例.

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

初始化后更改表单queryset.

Change the form queryset after initialization.

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

这篇关于Model.ManyToManyField.all()给出AttributeError:'ManyToManyDescriptor'对象没有属性'all'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆