django基础记述

ORM相关

Activating models,激活model

model生成数据库表格
python manage.py makemigrations + app名称(不加默认project更新)

Creating models,生成model对应的数据库表

python manage.py migrate

查看model文件的sql语句

python manage.py sqlmigrate ycapi_v2(app名称) 0001(文件名称)

内容提点:

  • 生成的表名 = app名称 + model名称(小写)
  • 如果不指定主键, 将会自动写入Primary keys(IDs)
  • django约定,外键格式为,后接有"_id" 的字段
  • 待翻译: The foreign key relationship is made explicit by a FOREIGN KEY constraint. Don’t worry about the DEFERRABLE parts; that’s just telling PostgreSQL to not enforce the foreign key until the end of the transaction.

检查没有数据库迁移的所有问题

'python manage.py check'

admin配置

创建用户

'python manage.py createsuperuser'

model向admin注册

进入 app名称文件夹/admin.py

from django.contrib import admin
from model文件 import model名称

admin.site.register(model名称)

单元测试相关

进入app名/tests.py
攥写测试用例:

import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

运行测试用例

python manage.py test + app名

你可能感兴趣的:(django基础记述)