Django Quick Build Blog section III (database table design)

Source: Internet
Author: User
Tags auth
The previous section we have been able to create a new blog app under the Pycharm, this time, we need to design a blog database designAbout the database table design, as a novice we do not need to understand too much to understand a little bit can also be, after all, the database also needs to have a certain foundation,

Here we are based on blog learning, the blog table design for three copies: Article table, label table, classification table

The structure looks like this:

The picture is also cut, compare ugly, here I follow my personal understanding, say:

Each article has a corresponding article ID, text, publication time, category, tags
It's reasonable, we can understand, but. Each article has some classification, label, this will produce duplicate data, resulting in the waste of resources, so we put the classification and tags out, separate tables, and then associated with the article. This will have the contents of Figure Ii.

2 Next we start creating database tables under Blog/models (note: Because of the ORM system used by Django (Object relational Mapping))
Paste the three table structure here:

From django.db Import Models # is specifically designed to handle the process of registering, logging in, and so on, user is the user model that Django has written for us.


From django.contrib.auth.models import User # Create your models here. # Classification database table class category (models. Model): name = models. Charfield (max_length=100) def __str__ (self): return Self.name # Tag database class tag (models. Model): name = models. Charfield (max_length=100) def __str__ (self): return Self.name # Article Database class post (models.
    Model): "Article database contains fields: Title, body creation time Created_time modified time modified_time Summary excerpt category category tag tag author author "' title = models. Charfield (max_length=100) BODY = models. TextField () Created_time = models. Datetimefield () Modified_time = models. Datetimefield () # Blank is true to indicate that the parameter can be null value za excerpt = models. Charfield (max_length=200,blank=true) # ForeignKey, that is, a one-to-many relationship (a category can have more than one article) Category = models. ForeignKey (Category) # and for labels, an article can have multiple labels, and there may be multiple articles under the same tag, # so we use Manytomanyfield to show that this is a many-to-many relationship. The article can be without tags,The blank can be true for tag = models.
    ForeignKey (tag,blank=true) # Author of the article, where User is imported from Django.contrib.auth.models.
    # Django.contrib.auth is a Django built-in app designed to handle the process of registering, logging in, and so on for website users, and the user is Django's model for what we've written.
    # Here we associate the article with the User through ForeignKey. Because we stipulate that an article can have only one author, and an author may write multiple articles, so this is a one-to-many association, and the Category is similar to author = models.
 ForeignKey (User) def __str__ (self): return Self.title

Later there will be a chance to improve the code display interface, if supported by the case, we put a picture of the code here

3 Synchronizing the database:

If you want to know how Django operates the database, you can use the command:

Here are the commands to use the database:

Python manage.py makemigrations
Python manage.py Migrate

When we see applying blog.0001_initial ... ok it means that the synchronization database was successful.

Python manage.py sqlmigrate Blog 0001

Django Command-line knowledge:

1 synchronizing databases: Python manage.py makemigrations
Python manage.py Migrate

2 See how Django operates the database: Python manage.py sqlmigrate blog 0001

The above is the design of database tables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.