Meta
options allow you to control various aspects of a model’s behavior that aren’t related to the data stored in the model fields themselves.
ordering
Example:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
class Meta:
ordering = ['-price']
Product
instances by price
in descending order (-price
).verbose_name
and verbose_name_plural
Example:
class Product(models.Model):
name = models.CharField(max_length=100)
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
Product
as the singular and plural name displayed in the admin interface.db_table
Example:
class Customer(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'app_customers'
app_customers
instead of the default app_customer
.unique_together
Example:
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField()
class Meta:
unique_together = ['customer', 'product']
customer
and product
is unique in the Order
model.