How does django use mysql database to query the database_Mysql database usage (Django), mysql, usage, django
One-to-many: Many-party definition foreign key!! # Student model class class Student( AbstractUser): mobile = models.CharField(max_length=11, unique=True, verbose_name='Mobile phone number') cid = models.ForeignKey(to=”Class”, to_field=”id”) class Meta: db_table = 'tb_student' verbose_name = 'user' verbose_name_plural = verbose_name # Class Model Class class Class(models.Model): id = models.AutoField(primary_key=True) cname &# 61; models.CharField(max_length=32) cdata = models.DateField() Implementing many-to-one in django When specifying , the usage is as follows: st = Student( username=student_name, password=password, cid=cl # cl is an instance of Class class ) In progress When searching : # One-to-many/many-to-one cl = Banji.objects.get(id& #61;3) tea = cl.student_set.all()[0] print(tea) print(cl.student_set .first()) st = Student.objects.get(username='fenghua') print(st.cid_id) print(st.cid.cname) Related query 1. Forward search (check more than 1): Get through the foreign key of the model class (here is cid) st.cid_id Find the class id corresponding to the student There is a field to_field=id when multiple parties define foreign keys in the model st.cid.cname Find the class name corresponding to the student 2. Reverse search (1 check more): If related_name is not set in the foreign key field& #xff0c;By default, the multi-party model lowercase _set is used. If related_name=”students”, is set, students can be used directly for reverse query cl (assumed to be a class object) cl.Model name lower case_set.first()…