Django_admin组件

Django_admin组件

作者:Eric
微信:loveoracle11g

新建Django项目bms图书管理系统

App为book

book.models.py添加表关系

from django.db import models

# Create your models here.

class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    # 与AuthorDetail建立一对一的关系
    authorDetail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)

class AuthorDetail(models.Model):
    nid = models.AutoField(primary_key=True)
    birthday = models.DateField()
    telephone = models.BigIntegerField()
    addr = models.CharField(max_length=64)

class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)
    email = models.EmailField()

class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    publishDate = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)
    # 与Publish表建立一对一的关系,外键字段建立在多的一方
    publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)
    # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
    authors = models.ManyToManyField(to="Author",)

敲两条命令迁移数据

F:\深圳骑士计划\crm\bms>python manage.py makemigrations
Migrations for 'book':
  book\migrations\0001_initial.py
    - Create model Author
    - Create model AuthorDetail
    - Create model Book
    - Create model Publish
    - Add field publish to book
    - Add field authorDetail to author

F:\深圳骑士计划\crm\bms>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, book, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying book.0001_initial... OK
  Applying sessions.0001_initial... OK

多出了一个db.sqlite3文件

http://127.0.0.1:8000/admin

它给我跳转到登录界面了,它里面用的是redirect

打开Run manage.py Task

输入命令

manage.py@bms > createsuperuser
输入用户名:yuan
邮箱:不写
输入密码:yuan1234
再次输入密码:yuan1234

猜你喜欢

转载自www.cnblogs.com/zhouwanchun/p/10691213.html