Performance: Through model for extra fields on M2M
MEDIUM IMPACT
This affects database query performance and page load speed by adding complexity to many-to-many relationships with extra fields.
class BookAuthor(models.Model): book = models.ForeignKey('Book', on_delete=models.CASCADE) author = models.ForeignKey('Author', on_delete=models.CASCADE) contribution = models.CharField(max_length=100) class Book(models.Model): authors = models.ManyToManyField('Author', through='BookAuthor')
class Book(models.Model): authors = models.ManyToManyField('Author') # Storing extra info in a separate model and querying manually
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No through model, manual extra data handling | Multiple queries increase DOM update delays | Multiple reflows due to incremental data | Higher paint cost due to delayed data | [X] Bad |
| Through model with optimized queries | Single query with joins reduces DOM delays | Single reflow after data load | Lower paint cost with faster data | [OK] Good |