美文网首页
Django一对多出版社与图书对应关系建立数据库表

Django一对多出版社与图书对应关系建立数据库表

作者: Steppe | 来源:发表于2020-01-08 15:13 被阅读0次

    models.py


    from django.dbimport models

    class Publisher(models.Model):

    name = models.CharField(max_length=32,verbose_name="出版社")

    class Meta:

    verbose_name ='出版社'

            verbose_name_plural = verbose_name

    def __str__(self):

    return self.name

    class Book(models.Model):

    title = models.CharField(max_length=32,verbose_name="书名")

    pub = models.ForeignKey(Publisher,on_delete=True,verbose_name="出版社外键")

    class Meta:

    verbose_name ='书籍'

            verbose_name_plural = verbose_name


    admin.py


    from django . contribimport admin

    from . models import Publisher,Book

    @admin . register(Publisher)

    class PublisherModel(admin.ModelAdmin):

    list_display = ["name"]

    @admin . register(Book)

    class BookModel(admin.ModelAdmin):

    list_display = ["title","pub"]


    apps.py


    from django.appsimport AppConfig

    class IndexConfig(AppConfig):

    name ='index'

        verbose_name ='书刊管理'


    settings.py


    INSTALLED_APPS = [

    'simpleui',没有这个模板的可进行安装pip install simpleui

        'django.contrib.admin',

        'django.contrib.auth',

        'django.contrib.contenttypes',

        'django.contrib.sessions',

        'django.contrib.messages',

        'django.contrib.staticfiles',

        'index.apps.IndexConfig',

    ]

    DATABASES = {

    'default': {

    'ENGINE':'django.db.backends.mysql',

            'HOST':'127.0.0.1',

            'PORT':'3306',

            'NAME':'waijian',

            'USER':'root',

            'PASSWORD':'qwe123',

        }

    }

    LANGUAGE_CODE ='zh-hans'

    TIME_ZONE ='Asia/Shanghai'


    相关文章

      网友评论

          本文标题:Django一对多出版社与图书对应关系建立数据库表

          本文链接:https://www.haomeiwen.com/subject/aglsactx.html