<small id='juMl2'></small><noframes id='juMl2'>

<tfoot id='juMl2'></tfoot>
<i id='juMl2'><tr id='juMl2'><dt id='juMl2'><q id='juMl2'><span id='juMl2'><b id='juMl2'><form id='juMl2'><ins id='juMl2'></ins><ul id='juMl2'></ul><sub id='juMl2'></sub></form><legend id='juMl2'></legend><bdo id='juMl2'><pre id='juMl2'><center id='juMl2'></center></pre></bdo></b><th id='juMl2'></th></span></q></dt></tr></i><div id='juMl2'><tfoot id='juMl2'></tfoot><dl id='juMl2'><fieldset id='juMl2'></fieldset></dl></div>

    1. <legend id='juMl2'><style id='juMl2'><dir id='juMl2'><q id='juMl2'></q></dir></style></legend>
        <bdo id='juMl2'></bdo><ul id='juMl2'></ul>

      1. Django Shortcut 嵌套外键

        Django Shortcut nested foreign key(Django Shortcut 嵌套外键)

        <legend id='qeBCb'><style id='qeBCb'><dir id='qeBCb'><q id='qeBCb'></q></dir></style></legend>
          <tbody id='qeBCb'></tbody>

            <small id='qeBCb'></small><noframes id='qeBCb'>

            <tfoot id='qeBCb'></tfoot>
            • <bdo id='qeBCb'></bdo><ul id='qeBCb'></ul>

              1. <i id='qeBCb'><tr id='qeBCb'><dt id='qeBCb'><q id='qeBCb'><span id='qeBCb'><b id='qeBCb'><form id='qeBCb'><ins id='qeBCb'></ins><ul id='qeBCb'></ul><sub id='qeBCb'></sub></form><legend id='qeBCb'></legend><bdo id='qeBCb'><pre id='qeBCb'><center id='qeBCb'></center></pre></bdo></b><th id='qeBCb'></th></span></q></dt></tr></i><div id='qeBCb'><tfoot id='qeBCb'></tfoot><dl id='qeBCb'><fieldset id='qeBCb'></fieldset></dl></div>
                  本文介绍了Django Shortcut 嵌套外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我的 models.py 中有以下内容:

                  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()
                  

                  我想做

                  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.

                  当然,我可以在课本上实现 get_chapters 方法,但我想避免这种情况.

                  Of course I could implement a method get_chapters on class book but i would like to avoid this.

                  有什么想法吗?

                  推荐答案

                  对 FK 字段使用相关名称参数,再加上查询集的 prefetch_related,将允许您以最小的性能损失获取与书籍相关的所有信息(每个 prefetch_related 参数调用一个单独的查询).

                  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.

                  但是,对于最高效的解决方案,还要保存一个 FK 以从章节中预订.这可以通过重写 save 方法轻松完成.

                  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 嵌套外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                • <i id='rlzCl'><tr id='rlzCl'><dt id='rlzCl'><q id='rlzCl'><span id='rlzCl'><b id='rlzCl'><form id='rlzCl'><ins id='rlzCl'></ins><ul id='rlzCl'></ul><sub id='rlzCl'></sub></form><legend id='rlzCl'></legend><bdo id='rlzCl'><pre id='rlzCl'><center id='rlzCl'></center></pre></bdo></b><th id='rlzCl'></th></span></q></dt></tr></i><div id='rlzCl'><tfoot id='rlzCl'></tfoot><dl id='rlzCl'><fieldset id='rlzCl'></fieldset></dl></div>
                      <tbody id='rlzCl'></tbody>
                    • <bdo id='rlzCl'></bdo><ul id='rlzCl'></ul>
                      <legend id='rlzCl'><style id='rlzCl'><dir id='rlzCl'><q id='rlzCl'></q></dir></style></legend>

                        <small id='rlzCl'></small><noframes id='rlzCl'>

                            <tfoot id='rlzCl'></tfoot>