Django 多对一的保存和查询

Django 多对一的保存和查询

Models 如下:

Python
from django.db import models class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() def __str__(self): return "%s %s" % (self.first_name, self.last_name) class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE) def __str__(self): return self.headline class Meta: ordering = ('headline',)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from django . db import models
 
class Reporter ( models . Model ) :
     first_name = models . CharField ( max_length = 30 )
     last_name = models . CharField ( max_length = 30 )
     email = models . EmailField ( )
 
     def __str__ ( self ) :
         return "%s %s" % ( self . first_name , self . last_name )
 
class Article ( models . Model ) :
     headline = models . CharField ( max_length = 100 )
     pub_date = models . DateField ( )
     reporter = models . ForeignKey ( Reporter , on_delete = models . CASCADE )
 
     def __str__ ( self ) :
         return self . headline
 
     class Meta :
         ordering = ( 'headline' , )
 

一篇文章 有多个记者

首先我们先保存记者的信息

Python
>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com') >>> r.save() >>> r2 = Reporter(first_name='Paul', last_name='Jones', email='paul@example.com') >>> r2.save()
1
2
3
4
5
6
>>> r = Reporter ( first_name = 'John' , last_name = 'Smith' , email = 'john@example.com' )
>>> r . save ( )
 
>>> r2 = Reporter ( first_name = 'Paul' , last_name = 'Jones' , email = 'paul@example.com' )
>>> r2 . save ( )
 

保存文章

Python
>>> from datetime import date >>> a = Article(id=None, headline="This is a test", pub_date=date(2005, 7, 27), reporter=r) >>> a.save() # 获取记者的id >>> a.reporter.id 1 # 获取记者的对象 >>> a.reporter <Reporter: John Smith>
1
2
3
4
5
6
7
8
9
10
>>> from datetime import date
>>> a = Article ( id = None , headline = "This is a test" , pub_date = date ( 2005 , 7 , 27 ) , reporter = r )
>>> a . save ( )
# 获取记者的id
>>> a . reporter . id
1
# 获取记者的对象
>>> a . reporter
< Reporter : John Smith >
 

通过记者新增一篇文章

Python
# 获取记者 >>> r = a.reporter # 根据记者 新增一篇文章 >>> new_article = r.article_set.create(headline="John's second story", pub_date=date(2005, 7, 29)) >>> new_article <Article: John's second story> >>> new_article.reporter <Reporter: John Smith> >>> new_article.reporter.id 1
1
2
3
4
5
6
7
8
9
10
11
# 获取记者
>>> r = a . reporter
# 根据记者 新增一篇文章
>>> new_article = r . article_set . create ( headline = "John's second story" , pub_date = date ( 2005 , 7 , 29 ) )
>>> new_article
< Article : John' s second story >
>>> new_article . reporter
< Reporter : John Smith >
>>> new_article . reporter . id
1
 
正向新增一篇文章
Python
>>> new_article2 = Article.objects.create(headline="Paul's story", pub_date=date(2006, 1, 17), reporter=r) >>> new_article2.reporter <Reporter: John Smith> >>> new_article2.reporter.id 1 # 根据记者获取记者下面的所有的文章 >>> r.article_set.all() <QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
1
2
3
4
5
6
7
8
9
>>> new_article2 = Article . objects . create ( headline = "Paul's story" , pub_date = date ( 2006 , 1 , 17 ) , reporter = r )
>>> new_article2 . reporter
< Reporter : John Smith >
>>> new_article2 . reporter . id
1
# 根据记者获取记者下面的所有的文章
>>> r . article_set . all ( )
< QuerySet [ < Article : John 's second story>, <Article: Paul' s story > , < Article : This is a test > ] >
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值