J'essaie de remplacer la fonction save
d'un modèle Django et envoyer des arguments supplémentaires de type mot-clé. Même si le code semble correct, j'ai obtenu cette erreur
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls. nom , TypeError : Student() a un argument mot-clé inattendu 'registration_no'
Mes modèles :
class StudentManager(models.Manager):
#check the availability of student
def CheckRegistration(self, name, registration_no):
print('inside CHECK REGISTRATION')
if Student.objects.filter(name=name).exists():
students = Student.objects.filter(name=name)
for student in students:
student_id = student.id
print(str(student_id))
registrations = Registration.objects.filter(student=student_id)
for registration in registrations:
print(str(registration))
if registration.registration_no==registration_no:
raise ValueError('student with same registration number already exist')
else:
print('registration possible')
return 1
class Student(models.Model):
name = models.CharField(max_length=300)
sex = models.CharField(choices=SEX_CHOICES,max_length=255, null=True)
Category = models.CharField(max_length=100, null=True)
objects = StudentManager()
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if(kwargs):
name = kwargs.get('name')
registration_no = kwargs.get('registration_no')
Student.objects.CheckRegistration(name, registration_no)
super(Student, self).save(*args, **kwargs)
RETOUR D'ERREUR
>>>Student.objects.create(name='AA',registrtion_no='BB')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/tluanga/.local/share/virtualenvs/alpha-back-0LxWEk3n/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/tluanga/.local/share/virtualenvs/alpha-back-0LxWEk3n/lib/python3.6/site-packages/django/db/models/query.py", line 420, in create
obj = self.model(**kwargs)
File "/home/tluanga/.local/share/virtualenvs/alpha-back-0LxWEk3n/lib/python3.6/site-packages/django/db/models/base.py", line 501, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Student() got an unexpected keyword argument 'registrtion_no'