This is a Django built-in table structure, which is to create an FK relationship for tables and N tables with two fields.
For example, there are two different courses, both of which have price cycles and strategies. If the lowest level is to create a price policy for each table. If you do not want to use a price policy in the same table,
So, in that case, course_id is associated with a topical course, the degree is empty, and if it is associated with a degree course, then the ID of the special course is empty, and if there are many courses, then
You need to write a lot of foreign key fields in the price strategy.
To solve this problem, Django introduced a way to be Django's contenttypes.
First, there is the following table structure:
fromDjango.dbImportModels fromDjango.contrib.contenttypes.modelsImportContentType fromDjango.contrib.contenttypes.fieldsImportGenericforeignkey, GenericrelationclassDegreecourse (models. Model):"""degree Programs"""name= Models. Charfield (max_length=128, unique=True) course_img= Models. Charfield (max_length=255, verbose_name="thumbnail Images") Brief= Models. TextField (verbose_name="Introduction to Degree programs", )classCourse (models. Model):"""Special Courses"""name= Models. Charfield (max_length=128, unique=True) course_img= Models. Charfield (max_length=255) #columns are not generated in the database and are only used to help you query policy_list = genericrelation ("pricepolicy") classPricepolicy (models. Model):"""Price and course validity list"""Content_Type= Models. ForeignKey (contenttype,on_delete=models. CASCADE)#Association course or Degree_courseobject_id =models. Positiveintegerfield ()#columns are not generated in the database and are only used to help you add and querycontent_object = Genericforeignkey ('content_type', 'object_id ' ) valid_period_choices= ( (1,'1 days'), (3,'3 days'), (7,'1 weeks'), (14,'2 weeks'), (30,'1 months'), (60,'2 months'), (90,'3 months'), (180,'6 months'), (210,'12 months'), (540,'18 months'), (720,'24 Months'),) Valid_period= Models. Smallintegerfield (choices=valid_period_choices) Price= Models. Floatfield ()
This structure is equivalent to:
Also create a table that holds the table name.
Then put 2 fields in the Price Strategy table, one field is the table name, and the other is the first data in the table.
For example, the first data in the course table is 21 days getting started.
Table Structure Analysis:
Content_Type = models. ForeignKey (contenttype,on_delete=models. CASCADE) # Association course or Degree_course
OBJECT_ID = models. Positiveintegerfield () #这个就是课程表内有多少课程, this constraint is not, can only be a positive integer.
The last is the query and the added test!
fromDjango.shortcutsImportRender,httpresponse fromApp01ImportModels fromDjango.contrib.contenttypes.modelsImportContentTypedefTest (Request):#1. Add a piece of data to the price policy table #models. PricePolicy.objects.create ( #valid_period=7, #price=6.6, #content_type=contenttype.objects.get (model= ' course '), #外键关联到这个表 #object_id=1 # )
#使用神奇的字段创建
#models. PricePolicy.objects.create ( #valid_period=14, #price=9.9, #Content_object=models. Course.objects.get (id=1) #这样得到就是Course表, and the ID is 1, two of these. Automatically adds these two values for you. # )
#2. According to a price strategy object, find his corresponding table and data, such as: Manage the course name #Price = models. PricePolicy.objects.get (id=2) #ID为2的这条价格策略对象 #print (price.content_object.name) # is associated with a special course that wants to show the name of a special course 21 days Getting Started
#3. Find all price policy databases associated with a course plus this policy_list=genericrelation ("Pricepolicy") #obj = models. Course.objects.get (id=1) #For item in Obj.policy_list.all (): #print (item.id,item.valid_period,item.price) # # returnHttpResponse ('...')
Add the two fields used by the relationship and use the Django.content-type table:
Not only is the price strategy, coupons will also use this, such as 2 types of courses, buy the first course has coupons, buy a second course may also have coupons.
Django's ContentTypes