3. Creating databases - Aklinahs/Django-beginner GitHub Wiki

Our website need four databases.

  1. User : To store the data about users
  2. Topic : to store the topics
  3. Room : to store the created rooms of topics
  4. Message : to store the comments of the room

1. User

User must contain following attributes

  1. name
  2. email
  3. Bio
  4. 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

image

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.

image

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

image

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)

image\

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

image

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

image

then update the admin.py as follows

image

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

  1. host
  2. topic
  3. name
  4. description
  5. participants
  6. updated
  7. created

create a room model as follows

image

then update the admin.py as follows

image

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

  1. user
  2. room
  3. body
  4. updated
  5. created

create a message model as follows

image

then update the admin.py as follows

image

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