.ORM a one-meter (OneToOneField)
1. Create the ORM table
And many-to-one relationship is very similar to the relationship. If you define a OneToOneField in the model, the model will be an example of the model through a simple attribute of the model associated with access.
class Person(models.Model): name = models.CharField(max_length=20) class PersonDetail(models.Model): age = models.IntegerField(10) email = models.EmailField() person = models.OneToOneField(Person) # person = models.ForeignKey(Person, unique=True)
2. Query
DEF One (Request):
# query user information alex # forward queries RET = models.PersonDetail.objects.get (the above mentioned id = 1) # RET = models.PersonDetail.objects.get (the above mentioned id = models.Person.objects.get (name = 'Alex'). ID) # Person here is a foreign key field in the table is not the class name Print (ret.person) # the Person Object of the Person object encapsulates Print (ret.person.name, ret.age , ret.email)
# query user information alex # reverse lookup obj = models.Person.objects.get (name = ' alex ' ) # personDetail class name in lowercase Print (obj.persondetail) # personDetail Object Print(obj.name, obj.persondetail.email, obj.persondetail.age) return HttpResponse('OK')
Reverse "query is different. Association model one to one relationship also has a manager object, but the manager represents a collection of objects rather than a single object. Is ForeignKey on the principle of one-way + unique = True. but since it is a foreign key query then the reverse should be one to one-to-many reverse lookup same way,
but in fact not the case, one-way reverse lookup is obj.persondetail object. class name in lowercase, no _set.
3. one table (case CRUD)
from django.db Import Models # parent table class Colors (models.Model): Colors = models.CharField (MAX_LENGTH = 10 ) DEF __str__ (Self): return self.colors # -one relationship, only one each of a ball colors, each ball has only one class ball (models.Model): # color of the ball-one correspondence with the color of the parent table color ball_color = models.OneToOneField ( ' colors ' ) # description description = models.CharField (max_length = 10 ) DEF __str__ (Self): return self.description
Query data
Table query sub-alphabet:
from myApp.models Import Colors, Ball
Method a: forward lookup
# inquiry "ball No. 1" corresponding to the color # obtain a child table object >>> B = Ball.objects.get (Description = " ball No. 1 " ) <ball: ball No. 1>
# obtain a corresponding master table object
>>> b.ball_color <Colors: Red>
method two: reverse lookup
# 获取“1号球”的 Colors 对象
>>> c = Colors.objects.get(ball__description="1号球")
<Colors: red>
>>> c.colors 'red'
Parent table query sub-tables:
Method a: Reverse Lookup #query corresponding to red ball description #obtain an Object Colors >>> C = Colors.objects.get (= Colors"Red") <Colors: Red>#obtain a corresponding ball objects #where the default lowercase names of sub-tables, if use is set related_name related_name >>>c.ball <ball: ball No. 1>
Method two: forward lookup
# 获取颜色为红的 Ball 对象
>>> b = Ball.objects.get(ball_color__colors="red")
<Ball: 1号球> >>> b.description '1号球'
adding data
# Create a new Colors object is c = Colors.objects.create (Colors = " Blue " ) # create a new Object Ball Ball.objects.create (ball_color = c, the Description = " 2 balls " )
delete data
# Delete a Ball objects Ball.objects.get (the Description = " 2 balls " ) .Delete () # remove a Colors objects Colors.objects.get (Colors = " Blue " ) .Delete () # Empty a table Colors.objects.all (). delete ()
change the data
c = Colors.objects.get(colors="red") c.colors = 'yellow' c.save()
# save() 方法 c = Colors.objects.create(colors="green") b = Ball.objects.get(description="1号球") b.color = c b.description = "3号球" b.save()
# Filter () method B = Ball.objects.filter (Description = " 3 Ball " ) # Note: update () and delete () method is QuerySet b.update (C = ball_color, Description = " Number 1 Ball " )