Django Models one-to-many operations

pay attention:

The new version of django needs to add on_delete=models.CASCADE when creating a new ForeignKey, ie

models.ForeignKey("UserType",on_delete=models.CASCADE) 


First, create a new models file
class UserInfo(models.Model):
name = models.CharField(max_length=10)
password = models.CharField(max_length=100)
user_type = models.ForeignKey("UserType",on_delete=models.CASCADE)


class UserType(models.Model):
id = models.AutoField(primary_key=True)
caption = models.CharField(max_length=10)


二,在views下添加数据
1.使用create添加数据
models.UserType.objects.create(caption="administrator") 

2. Add data using object management
  2.1
    obj = models.UserType() 
    obj.caption = "Super Administrator"
    obj.save()
  2.2
    obj1 = models.UserType(caption="normal user")
    obj1.save()

3. Add data using dictionary
  user_type_dict = {"caption":"打杂的"}
  models.UserType.objects.create(**user_type_dict)

  user_info_dict1 = {"name":"n1","password":"123","user_type":models.UserType.objects.get(caption="管理员")}
  models.UserInfo.objects.create(**user_info_dict1)

  user_info_dict2 = {"name": "n2", "password": "123", "user_type": models.UserType.objects.get(caption="超级管理员")}
  models.UserInfo.objects.create(**user_info_dict2)

  user_info_dict3 = {"name": "n3", "password": "123", "user_type": models.UserType.objects.get(caption="普通用户")}
  models.UserInfo.objects.create(**user_info_dict3)       print(obj.user_type.caption)      obj = models.UserInfo.objects.filter(name="n1").first()  1. Find other table data through foreign keys

Third, models forward lookup




      Find the data of the caption column of the UserType table through the UserInfo table, and use the foreign key user_type

  2. Through the powerful double underscore __ forward connection table operation
      obj = models.UserInfo.objects.filter(name="n1",user_type__caption=" Administrator").values("name","user_type__caption")
      print(obj.all().first().get("name"))
      through the filter filter conditions, operate through foreign key __ attributes of other tables For other tables, the difference between this and the first operation method (three [1]) is:
      this is to operate on objects, while before, the values ​​method was used to operate on specific data in a row to
      indicate which columns the new data has. values ​​will generate a dictionary, as well as a values_list, which will generate a tuple

four, models reverse lookup

  1. Reverse lookup
      obj = models.UserType.objects.filter(caption="administrator").first( through _set )
      print(obj.userinfo_set.all().first().password)
      The reverse lookup always uses the table name of other tables plus _set to select the column, and the forward lookup always uses the foreign key. In addition, userinfo_set What is generated is a special object djangoapp.UserInfo.None,
      
      pay special attention: when performing reverse lookup through _set, you must use the table name, not the class name, that is: there is case sensitivity
  2. Through the awesome double underscore __ reverse table operation 
      obj = models.UserType.objects.filter(caption="administrator").values("caption","userinfo__name")
      print(obj)
      through caption filter, when displaying values, uses table name + __ column name to perform reverse table join operation

summary: when using table join operation (ie: when you want to display data from multiple tables), use __ to For attribute calls between tables, foreign keys are used for forward lookups, and table names are used for reverse lookups.

   When table 1 calls the data in table 2, foreign keys are used for forward lookups. Attributes, and table names _set are used for reverse lookups to







add: The derivation of   one -       to



-many join   table operation in one-to-one join table

  operation .Use models.OneToOneField to create a one-to-one relationship, the principle is 1


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853738&siteId=291194637