Django之models database總結(jié)(四)

one-to-one 關(guān)系

定義一個one-to-one關(guān)系,使用OneToOneField,用起來和其他字段差不多。該字段需要一個位置參數(shù)用來指定要關(guān)系的model.
舉個簡單的例子:

from django.db import models

class Place(models.Model):
  name = models.CharField(max_length=50)
  address = models.CharField(max_length=80)
  def __str__(self): # __unicode__ on Python 2
    return "%s the place" % self.name 

class Restaurant(models.Model):
  place = models.OneToOneField(
    Place,
  on_delete=models.CASCADE,
    primary_key=True,
  )
  serves_hot_dogs = models.BooleanField(default=False)
  serves_pizza = models.BooleanField(default=False)
  def __str__(self): # __unicode__ on Python 2
    return "%s the restaurant" % self.place.name

class Waiter(models.Model):
  restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
  name = models.CharField(max_length=50)
  def __str__(self): # __unicode__ on Python 2
    return "%s the waiter at %s" % (self.name, self.restaurant)
 p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
 p1.save()
 p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
 p2.save()
 r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
 r.save()
 r.place
<Place: Demon Dogs the place>
 p1.restaurant
<Restaurant: Demon Dogs the restaurant>
 hasattr(p2, 'restaurant')
False
 r.place = p2
 r.save()
 p2.restaurant
<Restaurant: Ace Hardware the restaurant>
 r.place
<Place: Ace Hardware the place>
 p1.restaurant = r
 p1.restaurant
<Restaurant: Demon Dogs the restaurant>
 p3 = Place(name='Demon Dogs', address='944 W. Fullerton')
 Restaurant.objects.create(place=p3, serves_hot_dogs=True, serves_pizza=False)
Traceback (most recent call last):
...
ValueError: save() prohibited to prevent data loss due to unsaved related object 'place'.
 Restaurant.objects.get(place=p1)
<Restaurant: Demon Dogs the restaurant>
 Restaurant.objects.get(place__pk=1)
<Restaurant: Demon Dogs the restaurant>
 Restaurant.objects.filter(place__name__startswith="Demon")
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
 Restaurant.objects.exclude(place__address__contains="Ashland")
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>

 Place.objects.get(pk=1)
<Place: Demon Dogs the place>
 Place.objects.get(restaurant__place=p1)
<Place: Demon Dogs the place>
 Place.objects.get(restaurant=r)
<Place: Demon Dogs the place>
 Place.objects.get(restaurant__place__name__startswith="Demon")
<Place: Demon Dogs the place>

 w = r.waiter_set.create(name='Joe')
 w
<Waiter: Joe the waiter at Demon Dogs the restaurant>
 Waiter.objects.filter(restaurant__place=p1)
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>
 Waiter.objects.filter(restaurant__place__name__startswith="Demon")
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Django Model 定義語法 版本:1.7主要來源:https://docs.djangoproject.c...
    羅田閱讀 31,256評論 2 42
  • 點擊查看原文 Web SDK 開發(fā)手冊 SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個完善的 IM 系統(tǒng)...
    layjoy閱讀 14,418評論 0 15
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 原文:https://my.oschina.net/liuyuantao/blog/751438 查詢集API 參...
    陽光小鎮(zhèn)少爺閱讀 3,967評論 0 8
  • 來源網(wǎng)絡(luò) 懶人是不是就一定“慢活”?不一定。不過要想快活地“慢活”還是需要一點點技巧滴! 如果我告訴你:你可以什么...
    愛布袋閱讀 408評論 0 0

友情鏈接更多精彩內(nèi)容