mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-28 07:14:05 -06:00
Add Django Meta Class documentation
This commit is contained in:
parent
899c136ff2
commit
d7b06e4e00
@ -162,4 +162,40 @@ We have also added a __str__() method to the model class. This is the default Py
|
||||
![[Screenshot from 2023-12-22 11-33-17.png]]
|
||||
|
||||
|
||||
By default, Django adds an auto-incrementing primary key field to each model. The field type for this field is specified in each application configuration or globally in the DEFAULT_AUTO_FIELD setting. When creating an application with the startapp command, the default value for DEFAULT_AUTO_FIELD is BigAutoField. This is a 64-bit integer that automatically increments according to available IDs. If you don’t specify a primary key for your model, Django adds this field automatically. You can also define one of the model fields to be the primary key by setting primary_key=True on it.
|
||||
By default, Django adds an auto-incrementing primary key field to each model. The field type for this field is specified in each application configuration or globally in the DEFAULT_AUTO_FIELD setting. When creating an application with the startapp command, the default value for DEFAULT_AUTO_FIELD is BigAutoField. This is a 64-bit integer that automatically increments according to available IDs. If you don’t specify a primary key for your model, Django adds this field automatically. You can also define one of the model fields to be the primary key by setting primary_key=True on it.
|
||||
|
||||
# Django Meta Class
|
||||
|
||||
In Django models, the `Meta` class is a special inner class that you define within a model class to provide metadata about the model. This metadata can affect how the model behaves, how it's interpreted by Django's ORM (Object-Relational Mapping), and how it interacts with the database.
|
||||
|
||||
The `Meta` class is completely optional in a Django model. If you don't define one, Django will use default values for all the relevant settings. However, defining a `Meta` class gives you a lot of control over model-specific options. Some of the most commonly used options include:
|
||||
|
||||
1. **`ordering`**: This specifies the default order in which the objects returned by QuerySets should be sorted. For example, `ordering = ['-publish']` would order objects by the `publish` field in descending order.
|
||||
|
||||
2. **`db_table`**: This allows you to set a custom name for the database table. If this isn't provided, Django generates a default table name.
|
||||
|
||||
3. **`verbose_name`** and **`verbose_name_plural`**: These provide human-readable names for the model. This is especially useful in Django's admin interface.
|
||||
|
||||
4. **`unique_together`**: This can be used to define a composite unique constraint. For example, `unique_together = ('field1', 'field2')` ensures that each combination of `field1` and `field2` is unique across the database table.
|
||||
|
||||
5. **`index_together`**: Similar to `unique_together`, but for creating indexes in the database.
|
||||
|
||||
6. **`abstract`**: If set to `True`, the model is an abstract base class. Abstract base models are not used to create any database table. Instead, other models inherit from them and create database tables.
|
||||
|
||||
7. **`managed`**: If set to `False`, Django won't perform any database operations for the model (like creating the table, migrations, etc.). It's useful when the database table is managed outside of Django.
|
||||
|
||||
8. **`permissions`**: Custom permissions for the model, used in Django's authentication system.
|
||||
|
||||
9. **`constraints`**: Allows defining database constraints for the model.
|
||||
|
||||
10. **`get_latest_by`**: Field name to use with the model’s `get_latest_by()` method and `latest()` manager method.
|
||||
|
||||
11. **`default_related_name`**: The default reverse name to use for relationships pointing to this model.
|
||||
|
||||
12. **`indexes`**: Allows defining database indexes for the model.
|
||||
|
||||
13. **`app_label`**: Defines the name of the Django app that the model belongs to. This is useful if the model is defined outside of any application in `INSTALLED_APPS`.
|
||||
|
||||
These are just a few examples of what you can define in a `Meta` class. The options provide a powerful way to influence the SQL code generated by Django and how the ORM interacts with your models. Remember, any settings you define in a `Meta` class apply to the entire model class and all instances of that model.
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user