我似乎无法理解为什么我的ModelChoiceField不起作用,即使在阅读了关于ModelForm的django文档之后,尽管在我整个学习过程中都成功地依赖了这些文档,我还是感到困惑。我的代码中的错误可能很愚蠢,我对此表示歉意。
当我试图移民时,我在终端上遇到的错误是:
‘getattr中的第429行从None中生成AttributeError(名称):objects’
forms.py
from random import choices
from django import forms
from .models import Student
from students.choices import *
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__' #imports all of Student model's fields
labels = {
'student_number': 'Student Number', #changes how the attribute names are presented to the user
'first_name': 'First Name',
'last_name': 'Last Name',
'email': 'Email',
'english': 'English',
'math': 'Math',
'language': 'Language',
'science': 'Science',
'ib_predicted': 'Predicted IB Score'
}
widgets = {
'student_number': forms.NumberInput(attrs={'class': 'form-control'}),
'first_name': forms.TextInput(attrs={'class': 'form-control'}),
'last_name': forms.TextInput(attrs={'class': 'form-control'}),
'email': forms.EmailInput(attrs={'class': 'form-control'}),
'english': forms.ModelChoiceField(queryset=English.choices),
'science': forms.ModelChoiceField(queryset=Science.objects.all()),
'language': forms.ModelChoiceField(queryset=Language.objects.all()),
'math': forms.ModelChoiceField(queryset=Math.objects.all()),
'ib_predicted': forms.NumberInput(attrs={'class': 'form-control'})
}choices.py
from unittest.util import _MAX_LENGTH
from django.db import models
from django.utils.translation import gettext_lazy as _
class English(models.TextChoices):
LANGLIT = 'LL', _('Language and literature')
LIT = 'L', _('Literature')
class Math(models.TextChoices):
AA = 'AA', _('Analysis & Approaches')
AI = 'AI', _('Analysis & Interpretation')
class Language(models.TextChoices):
FRENCH = 'F', _('French')
SPANISH = 'S', _('Spanish')
ARABIC = 'A', _('Arabic')
MANDARIN = 'M', _('Mandarin')
class Science(models.TextChoices):
BIOLOGY = 'BIO', _('Biology')
CHEMISTRY = 'CHEM', _('Chemistry')
PHYSICS = 'PHY', _('Physics')
COMPUTERSCIENCE = 'CS', _('Computer Science')
DESIGNTECHNOLOGY = 'DT', _('Design Technology')
ESS = 'ESS', _('Environmental Systems and Societies')
class Society(models.TextChoices):
MANAGEMENT = 'BM', _('Business Management')
ECONOMICS = 'ECON', _('Economics')
GEOGRAPHY = 'GEO', _('Geography')
GLOBALPOLITICS = 'GP', _('Global Politics')
HISTORY = 'HIS', _('History')
PSYCHOLOGY = 'PSYCH', _('Psychology')
class Art(models.TextChoices):
VISUALARTS = 'VA', _('Visual Arts')
MUSIC = 'MUS', _('Music'),
FILM = 'FILM', _('Film')models.py
from unittest.util import _MAX_LENGTH
from django.db import models
from django.utils.translation import gettext_lazy as _
from students.choices import *
# Create your models here.
class Student(models.Model):
student_number = models.PositiveIntegerField()
first_name = models.CharField(max_length=50) #Attribute containing student's first name, cannot exceed 50 characters
last_name = models.CharField(max_length=50) #Attribute containing student's last name, cannot exceed 50 characters
email = models.EmailField(max_length=100) #Attribute containing student's email, cannot exceed 100 characters
ib_predicted = models.IntegerField()
#Subjects
english = models.CharField(max_length=2, choices=English.choices)
math = models.CharField(max_length=2, choices=Math.choices)
language = models.CharField(max_length=1, choices=Language.choices)
science = models.CharField(max_length=4, choices=Science.choices)
def __str__(self):
return f'{self.first_name} {self.last_name}'发布于 2022-10-17 19:05:06
您应该使用forms.ChoiceField而不是forms.ModelChoiceField。
当您想在ModelChoiceField或数据库Models中进行选择时,将使用Models。
使用forms.ChoiceField(choices=Math.choices)初始化您的选择
参考资料:https://docs.djangoproject.com/en/4.1/ref/forms/fields/#choicefield
https://stackoverflow.com/questions/74101842
复制相似问题