Почему я не могу получить доступ к идентификатору объекта в def clean(self)
? Я получаю сообщение об ошибке:
У объекта 'CheckInForm' нет атрибута 'id' .
forms.py
from django import forms
from .models import Student
from django.core.exceptions import ValidationError
class CheckInForm(forms.ModelForm):
class Meta:
model = Student
fields = ['room', 'name']
widgets = {'room': forms.TextInput(attrs={'class': 'form-control'}),
'name': forms.TextInput(attrs={'class': 'form-control'}),
}
def clean(self):
cleaned_data = super(CheckInForm, self).clean()
new_room = cleaned_data.get('room')
if Student.objects.filter(room=new_room).count() > 3:
print(Student.objects.filter(room=new_room).count())
if not Student.objects.filter(room=new_room, id=self.id):
raise ValidationError('The room is full')
models.py
class Student(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, db_index=True)
room = models.CharField(max_length=8, db_index=True, blank=True)