Issue
I am trying to define a foreign_key in a model in Django. But I am getting the error "TypeError: ForeignKey.init() got multiple values for argument 'on_delete'"
The way I define the field is like this:
class School(BaseModel):
student_number = models.ForeignKey(Student,_("student_number"), max_length=200,on_delete=models.CASCADE)
I don't really see why I am getting this error, because I have exact same implementation in other classes, and they do not raise an error.
Solution
The error you're encountering is due to the incorrect order of arguments in your ForeignKey field definition. The on_delete parameter should be specified after the target model (Student in this case). Here's the correct way to define the ForeignKey field:
class School(BaseModel): student_number = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name=_("student_number"), max_length=200)
In the corrected code:
on_delete=models.CASCADE comes after the target model (Student). verbose_name=_("student_number") is placed before max_length=200. This order is necessary to avoid the "TypeError: ForeignKey.init() got multiple values for argument 'on_delete'" error.
Ensure that you maintain a consistent order across all your model field definitions to avoid such errors. If other classes are working correctly, it's likely because they have the correct order of arguments. Update the School class as shown above, and the issue should be resolved.
Answered By - user22424929
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.