Я работаю над формой для сохранения данных, где от и до дней - поля выбора в models.py. Поэтому я делаю мою модель Model в forms.py полями выбора Я сделал это далеко.
models.py
class Create_Class(models.Model):
created_by = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, default=1)
class_name = models.CharField(max_length=150, null=True)
from_choice = (
('Mon', 'Monday'),
('Tue', 'Tuesday'),
('Wed', 'Wednesday'),
('Thu', 'Thursday'),
('Fri', 'Friday'),
('Sat', 'Saturday'),
('Sun', 'Sunday'),
)
from_days = models.CharField(max_length=10, choices=from_choice)
to_days = models.CharField(max_length=10, choices=from_choice)
from_time = models.TimeField()
to_time = models.TimeField()
created_on = models.DateTimeField(auto_now=True)
forms.py
class Create_Class_Model_Form(forms.ModelForm):
class Meta:
model = Create_Class
exclude = ['created_by', 'created_on']
fields = ['class_name', 'from_days', 'to_days', 'from_time', 'to_time']
from_choice = (
('Mon', 'Monday'),
('Tue', 'Tuesday'),
('Wed', 'Wednesday'),
('Thu', 'Thursday'),
('Fri', 'Friday'),
('Sat', 'Saturday'),
('Sun', 'Sunday'),
)
widgets = {
'class_name': forms.TextInput(attrs={'class': 'form-control'}),
'from_days': forms.ChoiceField(choices=from_choice, widget=forms.ChoiceWidget),
'to_days': forms.ChoiceField(choices=from_choice, widget=forms.Select()),
'from_time': forms.TimeInput(attrs={'class': 'form-control'}),
'to_time': forms.TimeInput(attrs={'class': 'form-control'}),
}
Views.py
def Class_create(request):
form = Create_Class_Model_Form(request.POST or None)
if form.is_valid():
obj = form.save(commit=False)
obj.created_by = request.user.username
obj.save()
print('successful')
messages.success(request, 'Saved Successfully')
c_names = Name_of_classes.objects.values('name')
templatename = 'genius/class_create.html'
context = {'form': form, 'c_names': c_names}
return render(request, templatename, context)
Ошибка
ChoiceField' object has no attribute 'is_hidden'
Ошибка от forms.py
Буду признателен за помощь в устранении этой ошибки.