Twelve .Django-to-many table ForeighKey (ORM)

I.-to-many table ForeighKey (ORM)

1. Create the ORM table

https://www.cnblogs.com/dangrui0725/p/9615641.html

 To-many: one-child table to select a data table from the parent, but the parent table of this data may also be other sub-table data selection 

in common is to add data in the admin, then, there will be a select box, but only the radio, because regardless of one to one or one to many that he is "a"
class Colors (models.Model): 
    Colors = models.CharField (MAX_LENGTH = 10) # Blue 
    DEF  __str__ (Self):
         return self.colors
 
class Clothes (models.Model): Color = models.ForeignKey ( " Colors " ) # the color table is a foreign key to the parent table color table description = models.CharField (MAX_LENGTH = 10) # description DEF __str__ (Self): return self.description

 Increased data

# Add the child table data, in the form of one to one and the same 
# add color to green clothing: the small guy 
# Method 1: 
models.Clothes.objects.create (Color = models.Colors.objects.get (Colors = " green " ), Description = " little guy " ) 
# 1 added: models.Clothes.objects.create (COLOR_ID = models.Colors.objects.get (= Colors " green " ) .id, Description = " little guy " )
# method 2: c_obj = models.Clothes (Color = models.Colors.objects.get (= Colors " green " ), Description = " little guy " ) c_obj.save ()

 Query data

# Foreign key table joint inquiries: 

# foreign key sub-table query alphabet, consistent with the one-child table query the parent table in the form of 
# find a color table red pants belongs colors - return: Red 
# written 1: 
Print (Models .Clothes.objects.get (Description = " little Hugo " ) .color.colors)   # returns the red, through the sub-master table lookup table, writing: "child table object name parent lowercase alphabet table entry field name.."; Clothes found by table description as "small Huge", find the corresponding Colors # writing 2, begin with the parent table: Print (models.Colors.objects.get (clothes__description = " little Hugo " ) .colors)   # returns red by sub-table query alphabet, but the form itself is direct access to the fields from the parent table objects, writing:. "alphabet .objects.get (sub-table name in lowercase __ child table field =" xxx ") parent table field name "; and upper exactly the same effect, another form # foreign key to the parent table query sub-tables, one with a different form, because the parent table as" more ", one is not the same as by .get () sub. table.Obtaining child table fields, but many to many table queries parent child table consistent 
#


Find all the clothing color is red - return: [<Clothes: great beauty>, <Clothes: Huge small>] # wording 1: color_obj = models.Colors.objects.get (Colors = " red " ) Print (color_obj. clothes_set.all ()) # Note: sub-table _set written in lowercase, and it is actually a QuerySet that, can update, delete, all, filter or the like

# writing 2: Print (models.Clothes.objects.filter (Color models.Colors.objects.get = (Colors = " red " )))
# writing 2 simple writing (recommended): Print (models.Clothes.objects.filter (color__colors = " red " )) # writing: filter (child table __ parent foreign key field = field "filter condition")

# written 3: = models.Colors.objects.get COLOR_ID (= Colors " red " ) .id # acquired by the color red is the parent table id Print (models.Clothes.objects.filter (COLOR_ID = COLOR_ID)) # filter obtained QuerySet, wording : filter (child table foreign key field = _ parent table's primary key master table primary key object)
 Note: By the QuerySet .values () method, the bis QuerySet into ValuesQuerySet method

Print
(models.Clothes.objects.filter (Color = models.Colors.objects.get (= Colors"red")) values (.'color__colors','description')) #acquisition sub-table description field, and a field master table of colors, wording obtain mother field: the name of the child table foreign key field female __ table field name - For values () or filter () #shorthand added:

Print(models.Clothes.objects.filter (color__colors ="red") .values ('color__colors','Description')) #returns: [{'Description ' : U ' \ u7ea2 \ u5185 \ u8863 ' , ' color__colors ' : U ' \ u7ea2 ' }, { ' Description ' : U ' \ u7ea2 \ u5185 \ u88e4 ' , ' color__colors ' : U ' \ u7ea2 ' }] # if not values (), returns the [<Clothes: great beauty>, <Clothes: small Huge>] such a QuerySet set form a list by values, each element in the list is a dictionary, by list () ValuesQeurySet into the list, then return to the Templates # Further by .values_list () into the QuerySet ValuesListQuerySet. Returns: [(u '\ u7ea2' , u '\ u7ea2 \ u889c \ u5b50'), # Get a list, the list is a plurality of tuples, each tuple is ValuesQuerySet value in the dictionary, the data used in the models was removed from the front end of the template dynamically added to the select option. # Pass by value forms.py models from the front select options, you need to restart the django, select the option to update, can be defined in the form, add the following keywords to protect dynamic update select option # forms.py from Django Import Forms from test1 Import Models class ClothesForm (forms.Form): Color = forms.IntegerField (required = True, the widget = forms.Select (),) DEF __init__ (Self, * args, ** kwargs): # define the key fields, when when using the form, colors table adds color, color options field of front-end ClothesForm automatically updated Super (ClothesForm, Self). __init__ (* args, ** kwargs) self.fields['color'].wi

change the data

# Color red clothing, description will be updated to big beautiful 
# written 1: 
models.Clothes.objects.filter (color__colors = " red " ) .Update (the Description = " big beautiful women " ) 
# writing 2: models.Clothes.objects .filter (COLOR_ID = models.Colors.objects.get (Colors = " red " ) .id) .Update (the Description = " big beautiful women " )

# written 3: colors_obj = models.Colors.objects.get (Colors = " red " ) colors_obj.clothes_set.filter (id__gte = 1) .Update (the Description = " big beautiful women " ) #Other modifications and wording referring to one of the foreign key query

delete data

models.Clothes.objects.get (Description = " gray skirt " ) .Delete () # objects and methods are QuerySet Delete ()
models.Colors.objects.filter (= Colors " gray " ) .Delete ()

 

Guess you like

Origin www.cnblogs.com/lovershowtime/p/11355656.html