Django Model Query All Fields Including The ManyToManyField In A Single Command
While playing around with the
ManyToManyField, I wondered is there a way you can query a
ManyToManyField automatically the same way you'd do a
class Author(models.Model): fullname = models.CharField(max_length=100) class Foo(models.Model): bar = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) foo = models.ForeignKey(Foo) author = models.ManyToManyField(Author)
In order to get the data I need from
Book I usually do:
book = Book.objects.select_related('foo').get(pk=1) authors = book.author.all()
which makes 2 trips. Is there a way to combine them the way
You can use
prefetch_related for M2M field
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module