为什么表单提交会产生AttributeError:“ NoneType”对象在Django中没有属性“ filter”

时间:2018-10-15 17:55:46

标签: python django

我不知道为什么这行不通。我的表单显示完美,但是当我按Submit时,出现以下错误:

AttributeError at /cycles/new/
'NoneType' object has no attribute 'filter'
Request Method: POST
Request URL:    http://127.0.0.1:8000/cycles/new/
Django Version: 2.1
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'filter'

以下是相应的文件摘录供参考。

models.py:

class Instructional_Cycle(models.Model):
    date_started = models.DateField(null=True)
    date_finished = models.DateField(null=True)
    standards = models.ManyToManyField(Standard, related_name="standards")

    class Meta:
        ordering = ['date_started']

    def __str__(self):
        return str(self.date_started) + ' to ' + str(self.date_finished)

    def get_absolute_url(self):
        return reverse('student_progress:cycle_detail', args=[str(self.id)])

class Standard(models.Model):

    subject = models.CharField(max_length=14, choices=subjects)
    grade_level = models.IntegerField(choices=gradeLevels)
    descriptor = models.CharField(max_length=15)
    description = models.TextField()
    essential_status = models.BooleanField(default=False)

    class Meta:
        ordering = ["subject", "grade_level", "descriptor"]

    def __str__(self):
        return self.descriptor + ': ' + self.description[:100]

    def get_absolute_url(self):
        return reverse('student_progress:standard_detail', args=[str(self.id)])

views.py:

class CycleCreateView(LoginRequiredMixin, FormView):
    model = Instructional_Cycle
    form_class = CycleForm
    template_name = 'cycle_new.html'
    success_url = reverse_lazy('student_progress:cycles')
    login_url = 'login'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet with extra items
        context['standards_list'] = Standard.objects.filter(essential_status=True)
        return context

forms.py:

class CycleForm(forms.Form):
    date_started = forms.DateField
    date_finished = forms.DateField
    standards = forms.ModelMultipleChoiceField(queryset=None)

我很确定表单正在返回我想要的值,因为在回溯中我可以看到:

[...]
File "/Users/xxx/.local/share/virtualenvs/mastery_tracker-pGN-zVGo/lib/python3.7/site-packages/django/forms/forms.py" in full_clean
381.         self._clean_fields()

File "/Users/xxx/.local/share/virtualenvs/mastery_tracker-pGN-zVGo/lib/python3.7/site-packages/django/forms/forms.py" in _clean_fields
399.                     value = field.clean(value)

File "/Users/xxx/.local/share/virtualenvs/mastery_tracker-pGN-zVGo/lib/python3.7/site-packages/django/forms/models.py" in clean
1293.         qs = self._check_values(value)

File "/Users/xxx/.local/share/virtualenvs/mastery_tracker-pGN-zVGo/lib/python3.7/site-packages/django/forms/models.py" in _check_values
1318.                 self.queryset.filter(**{key: pk})

Exception Type: AttributeError at /cycles/new/
Exception Value: 'NoneType' object has no attribute 'filter'

最后一步的本地变量是:

key 'pk'
pk  '378'
self    <django.forms.models.ModelMultipleChoiceField object at 0x10c6fdcf8>
value   frozenset({'378', '800', '858'})

是我选择包含在MtM字段中的记录的主键。请帮助我了解我在做什么错。搜索后找不到类似的问题/答案。谢谢!

0 个答案:

没有答案