zoukankan      html  css  js  c++  java
  • django建表

    建表语句

    1.手动建数据库

    2.在__init__文件中:

    Import pymysql
    pymysql.install_as_MySQLdb()

    3.在models文件中:

    from django.db import models
    
    # Create your models here.
    
    class Book(models.Model):
        id=models.AutoField(primary_key=True)
        name=models.CharField(max_length=32)
        price=models.DecimalField(max_digits=5,decimal_places=2)
        publish_date=models.DateField()
        publish=models.ForeignKey(to='Publish',to_field='id')
        authors=models.ManyToManyField(to='Author')
    
    class Publish(models.Model):
        id=models.AutoField(primary_key=True)
        name=models.CharField(max_length=32)
        city=models.CharField(max_length=32)
    
    
    class Author(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32)
        age=models.IntegerField()
      
    author_detail=models.OneToOneField(to="AuthorDetail",to_field='id')
    
    class AuthorDetail(models.Model):
        id = models.AutoField(primary_key=True)
        # name = models.CharField(max_length=32)
        telephone=models.BigIntegerField()
        addr=models.CharField(max_length=32)
        email=models.EmailField(null=True)
    
     

    4.settings中:

    DATABASES = {
    
            'ENGINE': 'django.db.backends.mysql',
    
            'NAME':'0113',
    
            'HOST':'127.0.0.1',
    
            'PORT':'3306',
    
            'USER': 'root',
    
            'PASSWORD': 'root',
    
    
    
        }
    
    }
     

    5.在迁移数据库

    python manage.py makemigrations
    
    python manage.py  migrate
  • 相关阅读:
    Java finally语句到底是在return之前还是之后执行?
    RedirectAttributes
    ueditor的使用
    controller跳到另一个controller
    $.post()用法例子
    进入一个jsp直接跳到另一个jsp
    mybatis多表查询
    asp.net在网页上显示数据库中的数据
    asp.net全局记住值
    面向对象
  • 原文地址:https://www.cnblogs.com/zhouhai007/p/10269125.html
Copyright © 2011-2022 走看看