Django Models
Django Models
Models in Django
In Django, A model is a class that represents table or collection in our DB, and
where every attribute of the class is a field of the table or collection. Models are
defined in the app/models.py (in our example: myapp/models.py).
Each model class maps to a single table in the database.
Django provides a big in-built support for database operations. Django provides
one inbuilt database sqlite3.
Django can provide support for other databases also like oracle,mysql,
postgresql, etc.
Model is defined in Models.py file. This file can contain multiple models.
Step 1:
you need to tell Django that you’re going to use them by registering the application name
in the INSTALLED_APPS list in the settings.py of the project:(Register app into the
INSTALLED_APPS inside settings.py file.
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
Field Options: You can set various options for each field, like max_length for
CharField or null and blank for allowing null values or empty strings.
Define Relationships (if needed):If your model has relationships with other
models (one-to-one, one-to-many, many-to-many), you can define them using
ForeignKey, OneToOneField, or ManyToManyField.
we are creating a model Employee which has Five fields id, name, city, mail and salary.
Model Class:
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
This command creates tables, modifies columns, adds indexes, and performs
any other database-related operations needed to reflect the changes you’ve
made.
The migrate command takes care of the order in which migrations are applied,
ensuring that dependencies between migrations are satisfied.
Once your models are defined and migrations are applied, you can use these models in your
views, serializers, forms, etc., to interact with the database.
Database Configuration:
Django by default provides sqlite3 database. If we want to use this database,we are
not required to do any configurations.
The default sqllite3 configurations in settings.py file are declared as follows.
If we don't want sqlite3 database then we have to configure our own database with the following
parameters.
1) ENGINE: Name of Database engine
2) NAME: Database Name
3) USER: Database Login user name
4) PASSWORD: Database Login password
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204
by Kunal Sir
Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk Road,
Karve Nagar, Pune, Maharashtra 411052 , Mobile No.- 8888022204