Create-new-Database
Create-new-Database
We can create the Database using the two ways - MySQL Workbench and
MySQL shell. MySQL Workbench is a GUI tool for the MySQL database that
provides features like SQL development, data modeling and server
administration. We will use the MySQL shell, which is more recommended for
learning purposes.
Use the create database my_database query. It will create the new
database.
1. +--------------------------------------+
2. | Database |
3. +--------------------------------------+
4. | information_schema |
5. | my_database |
6. | mysql |
7. | performance_schema |
8. | sys |
9. +---------------------------------------+
10. 5 rows in set (0.05 sec)
Once we are done creating the Database, we have to update the database
section of the settings.py file with the following setting configuration.
1. DATABASES = {
2. 'default': {
3. 'ENGINE': 'django.db.backends.mysql',
4. 'NAME': 'my_database',
5. 'USER': 'root',
6. 'PASSWORD': 'your_password',
7. 'HOST': '127.0.0.1',
8. 'PORT': '3306',
9. 'OPTIONS': {
10. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
11. }
12. }
13. }
Now we are ready to migrate or create tables in the newly created database.
In this final step, we will run the migrate command and it will create the
exiting tables in the my_database database.
After running this command Django will automatically create the necessary
tables such as auth_group, auth_user, auth_permission, etc. It will also
create the tables which are defined in the models.py file.
Database changed
mysql> show tables;
1. +-------------------------------------------------------+
2. | Tables_in_my_database |
3. +-------------------------------------------------------+
4. | auth_group |
5. | auth_group_permissions |
6. | auth_permission |
7. | auth_user |
8. | auth_user_groups |
9. | auth_user_user_permissions |
10. | django_admin_log |
11. | django_content_type |
12. | django_migrations |
13. | django_session |
14. | myweatherapp_profile |
15. +---------------------------------------------------------+
16. 11 rows in set (0.05 sec)
This is a built-in module and designed to perform admin related tasks to the
user.
Let's see how to activate and use Django's admin module (interface).
Django Model
In Django, a model is a class which is used to contain essential fields and
methods. Each model class maps to a single table in the database.
Model is defined in Models.py file. This file can contain multiple models.
Let's see an example here, we are creating a model Employee which has
two fields first_name and last_name.
The first_name and last_name fields are specified as class attributes and
each attribute maps to a database column.
This model will create a table into the database that looks like below.
The created table contains an auto-created id field. The name of the table is
a combination of app name and model name that can be changed further.
Register / Use Model
After creating a model, register model into
the INSTALLED_APPS inside settings.py.
For example,
ADVERTISEMENT
1. INSTALLED_APPS = [
2. #...
3. 'appname',
4. #...
5. ]
Field Options
Each field requires some arguments that are used to set column attributes.
For example, CharField requires mac_length to specify varchar database.
Choices An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this
field.
Default The default value for the field. This can be a value or a callable object.
help_text Extra "help" text to be displayed with the form widget. It's useful for
documentation even if your field isn't used on a form.
//models.py
1. class Student(models.Model):
2. first_name = models.CharField(max_length=20)
3. last_name = models.CharField(max_length=30)
4. contact = models.IntegerField()
5. email = models.EmailField(max_length=50)
6. age = models.IntegerField()