Django Develops Personal Blog Project-Blog Database Design

Welcome to my blog: Tribal Cabinet of Alpaca
Django operates the database through the Model. Whether your database type is MySql or Sqlite, Django automatically generates the corresponding database type of SQL statements for you, so you don't need to pay attention to the SQL statements and types, and Django automatically completes the operation of the data. Just write back the Model!

First of all, the list of blog articles must include the following points: the title of the blog, the content of the blog, the publishing time of the blog, the revision time of the blog, the classification of the blog, the clicks of the blog. Of course, there are bloggers in general, because we only publish articles on our website by ourselves, that is, we do not include other users, so the author can not write here. For the classification of blogs, we have a lot of websites to refer to, not to mention here, a blog can only have a classification, but can have multiple tags, such as the blog I write now, can be classified under django, but it can have multiple tags: django, blog, database, development...
Considering that each blog can only have one classification, and a classification can contain many blogs, so classification and blog is a one-to-many relationship, at this time we should use foreign keys to associate. And a blog can have multiple tags, each tag can also contain multiple blogs, so tags and blogs are many-to-many relationships. About one-to-many and many-to-many knowledge topics, here is no longer open, unfamiliar students can view django documents and related information.

Therefore, through the above analysis, we can identify three data tables: Blog, Category and Tag. The following three tables are created in models.py under the myblog directory. Since the blog table contains foreign keys and many-to-many relationships, two other tables should be created first:

Tag table:

class Tag(models.Model):
    """
    //Article label
    """
    name = models.CharField(verbose_name='Article label', max_length=20)
    number = models.IntegerField(verbose_name='Number of labels', default=1)
    class Meta:
        verbose_name = 'Article label'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

category table:

We use multi-level classification, namely django and Flask classification under Python classification. Here we need to use MPTT, we first install mptt in the virtual environment:

pip install django-mptt

Then mptt is registered in app:

INSTALLED_APPS = [
  ...
    'myblog',
     'mptt',
  
]

Back in models.py, import from mptt.models import MPTTModel

class Category(MPTTModel):
    name = models.CharField('Articles classification', max_length=50, unique=True)
    parent_name = models.ForeignKey('self', verbose_name='Category parent', null=True, blank=True, related_name='children', on_delete=models.CASCADE)
    number = models.IntegerField(verbose_name='Number of classifications', default=1)
    class Meta:
        db_table = 'category'
        verbose_name = verbose_name_plural = 'Articles classification'

    class MPTTMeta:
        parent_attr = 'parent_name'


    def __str__(self):
        return self.name

blog table:

In some blogs, we may need to use the article cover, but the article cover needs to have a place to upload, at this time we need to create an uploads folder in the project root directory, and then write the following at the bottom of settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')  #uploads must exist and be in the project directory
MEDIA_URL = '/uploads/'   #The files and images you upload will exist by default under / uploads/editor

Then add url.py in the blog folder:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
]
if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Create a blog table:

class Blog(models.Model):
    """
    //Blog
    """
    title = models.CharField(verbose_name='Title', max_length=100)
    abstract = models.TextField(verbose_name='abstract', max_length=200, default='', blank=True)
    img = models.ImageField(upload_to='article_img/%Y/%m/%d/', verbose_name='Articles and pictures', blank=True, null=True)
    content = models.TextField()
    create_time = models.DateTimeField(verbose_name='Creation time', default=timezone.now)
    modify_time = models.DateTimeField(verbose_name='Modification time', auto_now=True)
    click_nums = models.IntegerField(verbose_name='Click-throughput', default=0)
    category = models.ForeignKey(Category, verbose_name='Articles classification', on_delete=models.CASCADE)
    tag = models.ManyToManyField(Tag, verbose_name='Article label')


    class Meta:
        verbose_name = 'My blog'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

Last:

At Run manage.py Execution in Task

makemigrations    # Generate migration files

migrate    # Migrating data tables

You can add these three tables to our database.

Be careful:

We use mysql 5.7, but we don't use mysql at present. We use the sqllite database that comes with Django. We'll talk about configuring mysql database later.

Tags: Django Database MySQL SQL

Posted on Tue, 23 Jul 2019 18:15:56 -0700 by allspiritseve