图书管理系统

前期工作

更改数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'day56',
        'USER':'root',
        'PASSWORD':'123',
        'HOST':'127.0.0.1',
        'PORT':3306,
        'CHARSET':'utf8'
    }
}

环境配置

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]
settings.py
import pymysql
pymysql.install_as_MySQLdb()
__init__.py

表模型

from django.db import models

# Create your models here.

class MyChar(models.Field):
    def __init__(self,max_length,*args,**kwargs):
        self.max_length = max_length
        super().__init__(*args,**kwargs)

    def  db_type(self, connection):
        return 'char(%s)' % self.max_length


class Book(models.Model):
    title = models.CharField(max_length=64)
    price = models.DecimalField(max_digits=8,decimal_places=2)
    create_time = models.DateField(auto_now_add=True)

# 外键
    publish = models.ForeignKey(to='Publish')
    authors = models.ManyToManyField(to='Author')

    def __str__(self):
        return self.title


class Publish(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)

    def __str__(self):
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=64)
    age = models.IntegerField()

    author_detail = models.OneToOneField(to='')

    def __str__(self):
        return self.name

class AuthorDetail(models.Model):
    phone = models.BigIntegerField()
    addr = models.CharField(max_length=32)


    def __str__(self):
        return self.addr



 """
    models.py中的模型类__str__方法 必须返回一个字符串形式数据!!!

    """
models.py
01-23 06:36