Django之models database总结(四)

one-to-one 关系

定义一个one-to-one关系,使用OneToOneField,用起来和其他字段差不多。该字段需要一个位置参数用来指定要关系的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>]>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Django Model 定义语法 版本:1.7主要来源:https://docs.djangoproject.c...
    罗田阅读 31,152评论 2 42
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,767评论 0 15
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,161评论 19 139
  • 原文:https://my.oschina.net/liuyuantao/blog/751438 查询集API 参...
    阳光小镇少爷阅读 9,274评论 0 8
  • 来源网络 懒人是不是就一定“慢活”?不一定。不过要想快活地“慢活”还是需要一点点技巧滴! 如果我告诉你:你可以什么...
    爱布袋阅读 2,767评论 0 0

友情链接更多精彩内容