Performance: Model Meta class options
This affects database query efficiency and page load speed by controlling how Django ORM generates SQL and manages database indexes.
class MyModel(models.Model): name = models.CharField(max_length=100, db_index=True) class Meta: ordering = ['name'] # ordering on an indexed field
class MyModel(models.Model): name = models.CharField(max_length=100) class Meta: ordering = ['name'] # ordering on a non-indexed field
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Ordering on non-indexed field | N/A | N/A | Increases server response time | [X] Bad |
| Ordering on indexed field | N/A | N/A | Faster server response | [OK] Good |
| No unique constraints | N/A | N/A | Slower queries, possible data errors | [X] Bad |
| Unique constraints with indexes | N/A | N/A | Faster queries, data integrity | [OK] Good |
| Default db_table name | N/A | N/A | Potential migration delays | [!] OK |
| Explicit db_table name | N/A | N/A | Faster deployment, fewer conflicts | [OK] Good |
Meta class inside a Django model?db_table.db_table is valid; others like table_name, database_table, and table are not recognized by Django.class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
class Meta:
ordering = ['-price', 'name']
['-price', 'name'] means first order by price descending (because of the minus), then by name ascending.class Meta:
ordering = '-created_at'
verbose_name = 'Blog Post'
ordering option must be a list or tuple of field names, not a single string.verbose_name is correctly a string; Meta must be inside the model; fields are not defined in Meta.Article has a unique combination of author and title. Which Meta option correctly enforces this at the database level?unique_together.unique_fields, unique, and unique_constraint are not valid Django Meta options.Pass the test and this pattern moves unknown