A space to discuss and keep up software development and manage your software career
An inclusive community for gaming enthusiasts
News and discussion of science and technology such as AI, VR, cryptocurrency, quantum computing, and more.
From composing and gigging to gear, hot music takes, and everything in between.
Memes and software development shitposting
Discussing AI software development, and showing off what we're building.
Movie and TV enthusiasm, criticism and everything in-between.
Web design, graphic design and everything in-between
A community for makers, hobbyists, and professionals to discuss Arduino, Raspberry Pi, 3D printing, and much more.
For engineers building software at scale. We discuss architecture, cloud-native, and SRE—the hard-won lessons you can't just Google
Discussing the core forem open source software project — features, bugs, performance, self-hosting.
A collaborative community for all things Crypto—from Bitcoin to protocol development and DeFi to NFTs and market analysis.
You can use QuerySet.prefetch_related or QuerySet.select_related with Abstract Base Classes in Django.
QuerySet.prefetch_related
QuerySet.select_related
Let's say we have these models:
class Country(models.Model): name = models.CharField(max_length=200) def __str__(self): return self.name class AbstractPerson(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) class Meta: abstract = True class AbstractAddress(models.Model): street = models.CharField(max_length=100) city = models.CharField(max_length=50) state = models.CharField(max_length=2) zip_code = models.CharField(max_length=10) country = models.ForeignKey(Country, on_delete=models.CASCADE) class Meta: abstract = True class AbstractPhoneNumber(models.Model): phone_number = models.CharField(max_length=15) class Meta: abstract = True class Contact(AbstractPerson, AbstractAddress, AbstractPhoneNumber): email = models.EmailField() def __str__(self): return f"{self.first_name} {self.last_name} - {self.country}"
If you make this query you wouldn't have a N+1 issue:
> Contact.objects.select_related("country").get(id=1).country
You have one query only. The generated SQL is:
SELECT app_contact.id, app_contact.first_name, app_contact.last_name, app_contact.street, app_contact.city, app_contact.state, app_contact.zip_code, app_contact.country_id, app_contact.phone_number, app_contact.email, app_country.id, app_country.name FROM app_contact INNER JOIN app_country ON (app_contact.country_id = app_country.id) WHERE app_contact.id = '1'
Maybe you were referring to something else?
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can use
QuerySet.prefetch_related
orQuerySet.select_related
with Abstract Base Classes in Django.Let's say we have these models:
If you make this query you wouldn't have a N+1 issue:
You have one query only. The generated SQL is:
Maybe you were referring to something else?