3. Creating databases - Aklinahs/Django-beginner GitHub Wiki
Our website need four databases.
- User : To store the data about users
- Topic : to store the topics
- Room : to store the created rooms of topics
- Message : to store the comments of the room
1. User
User must contain following attributes
- name
- Bio
- avatar
email must be unique to each user,
Here have to create a custom user model (inherit from default user model).
go to the base\models.py and create the user model
after creating the coustom user model, we have to tell django to use this model instead of the original django user model. update settings.py defining the **AUTH_USER_MODEL**
property.
To create new migrations of the user model ,
in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter
python manage.py makemigrations
command
Then apply the migrations using
python manage.py migrate
command
register user model at admin panel.
to view the created model in django admin panel, we have to inform it, about our new model. to do this, open the base\admin.py
and add flowing code
after creating a data model, we can inspect it using django admin pannel. to do that, activate virtual env and go to the studybud directory.
python manage.py createsuperuser
now enter preferred username, email and password.
this should be done ideally in the beginning of a project and with an extra care. It will change the whole database schema.
lets add new fields as we need.
So the usermodel looks like this (comment out avatar field for later complete)
\
To create new migrations of the user model ,
in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter
python manage.py makemigrations
command
Then apply the migrations using
python manage.py migrate
command
now you can open your web browser and go to the [http://127.0.0.1:8000/admin/](http://127.0.0.1:8000/admin)
and login using your email and password
add avatar to the user
uncomment the avatar at the user model.
avatar = models.ImageField(null=True, default='avatar.svg')
The imageField() is relied on third party package named pillow. So we have to download and install pillow package.
To install pillow run the following command
python -m pip install pillow
to set default image , we have to specify the link to our default image. the best practice is create a new folder in root folder (where manage.py is at). name it as static, this is the place we are going to keep all of our statics files
add preferred default avatar image here.
now we have to inform settings.py that our media files are at this folder.
to save user uploaded content, we have to make a new folder inside the inside folder, lets name it as images and specify the path in settings.py, to do so add the following line
MEDIA_ROOT = BASE_DIR / 'statics/images'
next we have to specify the media URL to those images, so add following tine to the settings.py
MEDIA_URL = '/images/'
MEDIA_ROOT is for server path to store files in the computer. MEDIA_URL is the reference URL for browser to access the files over Http.
We also need to configure the root directories' urls.py file
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
To create new migrations of the user model , in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter
python manage.py makemigrations
command
Then apply the migrations using
python manage.py migrate
command
2.Topic model
Topic model just contain the name , So it would be like
then update the admin.py as follows
To create new migrations of the user model , in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter python manage.py makemigrations command
Then apply the migrations using python manage.py migrate command
3. room
room must contain followings
- host
- topic
- name
- description
- participants
- updated
- created
create a room model as follows
then update the admin.py as follows
To create new migrations of the user model , in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter python manage.py makemigrations command
Then apply the migrations using python manage.py migrate command
4. messages
must contain followings
- user
- room
- body
- updated
- created
create a message model as follows
then update the admin.py as follows
To create new migrations of the user model , in command prompt change directory to the studybud and, activate the virtual environment. Then type and enter python manage.py makemigrations command
Then apply the migrations using python manage.py migrate command