Django Shortcut嵌套外键 [英] Django Shortcut nested foreign key
问题描述
假设我的models.py中有以下内容:
class书籍:经过课堂部分:book = models.ForeignKey(书籍)课章:零件=模型.ForeignKey(零件)数字= models.IntegerField()
我想做
book = Book.objects.get(id = someID)章节= Book.chapters.get(number = 4)
什么是干净的方法?我在这本书上想到了一位经理,但在这种情况下似乎不起作用.
我当然可以在课本上实现get_chapters方法,但我想避免这种情况.
有什么想法吗?
您也可以在模板中执行此操作.
但是,对于性能最高的解决方案,也请保存FK以便从章节中进行预订.通过覆盖save方法可以轻松完成此操作.
class章节:part = models.ForeignKey(Part,related_name ="part_chapters")数字= models.IntegerField()book = models.ForeignKey(Book,related_name ="chapters",null = True,blank = True)#允许null/blank值;将在保存方法中填充def save(self,* args,** kwargs):self.book = self.part.book超级(第章,自我).save(* args,** kwargs)>>>book = Book.objects.first().prefetch_related("parts","chapters")>>>print(book.parts.all())#返回书籍的所有部分>>>打印(book.chapters.all())
Suppose I have the following in my models.py:
class Book:
pass
class Part:
book = models.ForeignKey(Book)
class Chapter:
part = models.ForeignKey(Part)
number = models.IntegerField()
I would like to do
book = Book.objects.get(id=someID)
chapters = Book.chapters.get(number=4)
What is a clean way to do so? I thought of a Manager on the book class but it doest not seem to work for this case.
Of course I could implement a method get_chapters on class book but i would like to avoid this.
any ideas ?
Using related_name arguments for FK fields, coupled with prefetch_related for a queryset, will allow you to fetch all info related to a book with minimal performance hit (each prefetch_related param calls a separate query).
class Book:
pass
class Part:
book = models.ForeignKey(Book, related_name="parts")
class Chapter:
part = models.ForeignKey(Part, related_name="chapters")
number = models.IntegerField()
# fetch a book and all related info w/ only 2 db hits
book = Book.objects.first().prefetch_related("parts","parts__chapters")
print(book.parts.all()) # returns all parts for book
for part in book.parts.all():
print part.chapters.all()
You can do this in a template as well.
However, for the most performant solution, save a FK to book from chapter as well. This can be easily done by overriding the save method.
class Chapter:
part = models.ForeignKey(Part, related_name="part_chapters")
number = models.IntegerField()
book = models.ForeignKey(Book, related_name="chapters", null=True, blank=True) # allow null/blank values; will be populated in save method
def save(self, *args, **kwargs):
self.book = self.part.book
super(Chapter, self).save(*args, **kwargs)
>>> book = Book.objects.first().prefetch_related("parts","chapters")
>>> print(book.parts.all()) # returns all parts for book
>>> print(book.chapters.all())
这篇关于Django Shortcut嵌套外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
相关文章
- Django Shortcut 嵌套外键;
- Django快捷方式嵌套外键;
- Django - 使用2个嵌套外键复制模型实例;
- Django外键只读;
- django外键保存;
- 外键Django模型;
- 外键 Django 模型;
- django 外键保存;
- Django:外键查询;
- Django CreateView外键;
- Django外键下拉;
- Django Queryset外键;
- Django表与外键;
- django 外键模型计数;
- 双重外键在Django?;
- Django:独特的外键;
- Django过滤查询外键;
- Django:不同的外键;
- python - Django外键操作;
- Django - 获取外键模型;
- Django外键关系模板;
- django外键关系查找;
- Django - 过滤外键属性;
- Django自引用外键;
- Django - 过滤外键属性;
Python最新文章
- 类型错误:只有长度为1的阵列可以尝试拟合指数的数据转换到Python标量;
- bs4.FeatureNotFound:找不到一棵树建设者您所要求的功能:LXML。你需要安装一个解析器库?;
- 系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all();
- (unicode错误)'unicodeescape'编解码器无法解码位置2-3中的字节:truncated \UXXXXXXXX escape;
- 将pandas dataframe中的列从int转换为string;
- Python:由实例对象调用方法:“missing 1 required positional argument:'self'”;
- Sparksql过滤与多个条件(与where子句中选择);
- JSONDe codeError:期待值:1行1列(CHAR 0);
- Cmake不能找到Python库;
- Python - 将Dataframe中的所有项目转换为字符串;