Foreign key self-association and reference undefined Model methods of Mode in Django
This article introduces the relevant knowledge of "self-correlation of foreign keys in Mode in Django and citing undefined Model methods". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Foreign key autocorrelation of Django Model
In the model definition of django, it is sometimes necessary for a Field to refer to the currently defined Model, such as the Model of a department (Department), which has a field of a superior department (super_department), which should be a foreign key and reference Model Department, that is:
Class Department (models.Model):''some other filed' super_department = models.ForeignKey (Department)
But such a definition is not allowed for python, because when defining Field super_department, the Department definition is not complete, and python will prompt you that Department is not defined, so what should I do? Don't worry, django has already considered this situation for you. You just need to change the Model referenced by ForeignKey to 'self'' when defining it, that is:
Class Department (models.Model):''some other filed' super_department = models.ForeignKey ('self')
Then when you synchronize the models to the database, a foreign key that references itself is generated.
Django Model references an undefined Model
Sometimes an undefined Model is referenced in the Model definition of django. For example, a user (User) belongs to a department (Department), and each department has a leader (leader), who is also a user, that is:
Class Department (models.Model): name = models.CharField (max_length=20) leader = models.ForeignKey (User, related_name = 'lead_group', null = True) class User (models.Model): username = models.CharField (max_length=20) department = models.ForeignKey (Department, related_name =' users')
However, this definition will cause Field leader to reference User when Department is defined, but the User has not been defined at this time. Some people will say that defining User first and then defining Department will cause the same problem, because the department of User refers to Department, and Department is also undefined at this time. So what should we do at this time? In fact, you only need to change the Model referenced by ForeignKey to a string, that is:
Class Department (models.Model): name = models.CharField (max_length=20) leader = models.ForeignKey ('User', related_name =' lead_group') # User now be str type class User (models.Model): username = models.CharField (max_length=20) department = models.ForeignKey (Department, related_name = 'users') "this is the end of the introduction of Mode's foreign key self-association and reference undefined Model methods in Django. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!