Step by Step Guide on how to set up a Django database Pt.2(Model, View and Serializer)
Tags: django, python
Published: 2020-07-29 01:24:36

Welcome back! Here's part 2 of my How-to set up a Django database:
------------------------------
WARNING/EDIT: If you are creating a Database with Users/Auth, you will need to do these steps, BEFORE migrating for the first time! If you have already gone too far, I will show how to remedy this in my Django Auth Blog.

Directly from Django Docs:
Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.

You will need to put this in your Settings.py in your Project Folder. It should be AUTH_USER_MODEL = [your_app_name.user]:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/atikwev4bpkzjlteqfmz.png)

Additionally, add this to your Models(You can customize further later on, I will be going into detail in my Django Auth installment):
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e5rn8zmuly55le9v0euy.png)

Then, you will need to register your user model in your Admin.py:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/arakdcczqtwegg2iv2yf.png)

------------------------------

For starters, let's make sure that the file structure you have is correct. You will need to have the master folder contain the src and venv folders, within the src folder you should have the  manage.py, project and app folders. Like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wu2xv50913b74tbi1uin.png)

Once you have confirmed your file structure is correct- Django is VERY particular and you need to have it set up in a way that enables you to run the manage.py and have access to your project/app folders- we can start on the first aspect, the Model!

Important Note: Django's syntax is built around indentation. There is no curly braces or 'end'. You need to make sure that your classes and methods are indented correctly in order for them to run. 

Go to your models.py in your app folder. There we will be writing the classes and their correlating fields (basically columns that will be storing information in your Database). For my Model, I included a created_at field where it is using a time stamp. At the top of my file, I included:
from django.utils import timezone
import datetime

This is only needed if you would like to include DateTime.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xc4g88bk3cyiazs4vl0r.png)

In the Model, in addition to the fields you will need to add the data type that the information will be stored in. The most common is CharField, which requires a max_length attribute. 

TextField is for much longer text, like paragraphs. It will still store TextField as a string, but does not need any attributes. 

Finally, the DateTime field includes auto_now_add:True, which means the time stamp will be added automatically at creation. The null=True is set in the event the created_at is not existent, it will still create the instance. Finally, the blank=True means that is can be left blank and not required in your admin or custom forms. 

Next, go to your admin.py file and register your models:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8cmesn2w7l5ypt56bl6z.png)

Once that is completed, the next step is to create the Serializer! In your command line, cd into your app folder and create a serializers.py file. Run: touch serializers.py. Then, cd back into your src folder when you are done and create your serializer:

The Serializer includes just the models and fields, there are additional attributes that can be added to customize your Serializer. One note is that Django does not automatically create an id(the Primary Key) for each instance, instead it leaves it open for customization. I included an id, because I like to have that field as my Primary Key, but it is completely up to you!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/efl2mp91m0okg6nfnwzu.png)

Next we have the View! The view determines how the class will be displayed. You will need to include the queryset and serializer_class(this will provide the information needed to render the data). Also, be sure to import the models and serializers into your view file:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ay77q7xma8xm0wulvxud.png)

Finally, we will need to create routes for our views! This is done in both the urls.py files in your app and project folders. 

First, in your app folder register your router. There will be a router.register for each model you have:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wofze1bdxgeko3gck2ne.png)

Next, go into your urls.py in your project folder and include the urls from your app:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fqcmkhlsgnmic8renlvg.png)

Hooray! Everything is set up! Be sure to run your server and check your Admin site to confirm everything is showing and perform your CRUD operations!

*Be on the Look Out for my next installment in this series: Django Auth*

Feel free to check out my github repo: https://github.com/kathryn0908/django_blog 

Happy Coding!