Reputation: 201
I am trying to override the save
method of a Django model and send extra keyword arguments. Even though the code looks fine but i got this error
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.name, kwarg)) TypeError: Student() got an unexpected keyword argument 'registration_no'
My models:
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)
ERROR-TRACEBACK
>>>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'
Upvotes: 2
Views: 6087
Reputation: 752
It seems like you have a typo in writing 'registrtion_no' instead of 'registration_no', so use this instead:
Student.objects.create(name='AA',registration_no='BB')
Hope this helps!
If it does not, please post your code for the 'create' function
Upvotes: 3