举例讲解Django中数据模型访问外键值的方法
作者:goldensun 时间:2022-05-29 18:40:11
先设置一个关于书本(book)的数据模型:
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
访问外键(Foreign Key)值
当你获取一个ForeignKey 字段时,你会得到相关的数据模型对象。 例如:
>>> b = Book.objects.get(id=50)
>>> b.publisher
<Publisher: Apress Publishing>
>>> b.publisher.website
u'http://www.apress.com/'
对于用`` ForeignKey`` 来定义的关系来说,在关系的另一端也能反向的追溯回来,只不过由于不对称性的关系而稍有不同。 通过一个`` publisher`` 对象,直接获取 books ,用 publisher.book_set.all() ,如下:
>>> p = Publisher.objects.get(name='Apress Publishing')
>>> p.book_set.all()
[<Book: The Django Book>, <Book: Dive Into Python>, ...]
实际上,book_set 只是一个 QuerySet,所以它可以像QuerySet一样,能实现数据过滤和分切,例如:
>>> p = Publisher.objects.get(name='Apress Publishing')
>>> p.book_set.filter(name__icontains='django')
[<Book: The Django Book>, <Book: Pro Django>]
属性名称book_set是由模型名称的小写(如book)加_set组成的。
标签:Django,数据模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
css3弹性盒模型
2010-05-10 20:47:00
![](https://img.aspxhome.com/file/UploadPic/20105/10/01-33s.jpg)
CGArt®2008 贺岁刊电子杂志玉鼠闹春
2008-02-15 08:59:00
![](https://img.aspxhome.com/file/UploadPic/20082/15/200821511345421s.jpg)
keras model.fit 解决validation_spilt=num 的问题
2023-11-07 19:11:52
![](https://img.aspxhome.com/file/2023/2/87482_0s.jpg)
Python查询缺失值的4种方法总结
2023-10-29 13:42:08
![](https://img.aspxhome.com/file/2023/1/90871_0s.png)
使用Fabric自动化部署Django项目的实现
2022-09-05 22:00:33
改变 Python 中线程执行顺序的方法
2022-01-14 16:11:10
![](https://img.aspxhome.com/file/2023/4/100974_0s.gif)
python 用pandas实现数据透视表功能
2022-04-23 02:49:40
![](https://img.aspxhome.com/file/2023/7/65217_0s.png)
Pytorch之Variable的用法
2022-01-19 04:16:39
VS Code有哪些奇技淫巧(必知)
2022-01-07 21:10:47
![](https://img.aspxhome.com/file/2023/7/100847_0s.png)
python 字典中文key处理,读取,比较方法
2021-11-30 20:42:36
![](https://img.aspxhome.com/file/2023/4/72094_0s.jpg)
Linux服务器网卡流量查看方法 shell和Python各一枚
2023-10-07 09:31:19
![](https://img.aspxhome.com/file/2023/9/66259_0s.gif)
asp+XMLHTTP的国际域名查询系统(whois)源代码
2008-02-16 09:23:00
PyQt5实现暗黑风格的计时器
2023-07-07 02:49:30
![](https://img.aspxhome.com/file/2023/0/61990_0s.jpg)
html网页颜色表大全(苏昱)
2008-01-01 15:52:00
![](https://img.aspxhome.com/file/UploadPic/20081/1/200811162311168s.gif)
从SNS看社会化界面设计(一)
2009-02-23 12:21:00
![](https://img.aspxhome.com/file/UploadPic/20092/23/1-73s.jpg)
python特效之字符成像详解
2021-06-07 02:18:45
![](https://img.aspxhome.com/file/2023/0/80400_0s.jpg)
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
Python读写JSON文件的操作详解
2021-03-22 11:46:35
![](https://img.aspxhome.com/file/2023/0/82670_0s.jpg)
Python MD5文件生成码
2023-01-21 02:41:31
处理Python中的URLError异常的方法
2021-06-04 03:33:15