zoukankan      html  css  js  c++  java
  • django-orm基础字段及选项1

    orm基础字段及选项

    字段类型

    from django.db import models
    
    # Create your models here.
    class Book(models.Model):
        title=models.CharField('书名',max_length=50,default='')
        price=models.DecimalField('价格',max_digits=7,decimal_places=2)
        info=models.CharField('描述',max_length=100,default='')
    
    class Author(models.Model):
        name=models.CharField('姓名',max_length=11)
        age=models.IntegerField('年龄')
        email=models.EmailField('邮箱')
    
    


    blank主要控制admin后台值为空,与null有一定区别
    image
    image
    image

    添加修改字段都需要makemigrations 和migrate

    针对模型类表的操作

    meta类
    image

    # Create your models here.
    class Book(models.Model):
        title=models.CharField('书名',max_length=50,default='')
        price=models.DecimalField('价格',max_digits=7,decimal_places=2)
        info=models.CharField('描述',max_length=100,default='')
        class Meta:
            db_table='book'
    

    ····
    C:UsersAdministratorDesktopmysite2>python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, auth, bookstore, contenttypes, sessions
    Running migrations:
    Applying bookstore.0002_author... OK

    C:UsersAdministratorDesktopmysite2>python manage.py makemigrations
    Migrations for 'bookstore':
    bookstoremigrations003_auto_20210807_1002.py
    - Rename table for book to book

    C:UsersAdministratorDesktopmysite2>python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, auth, bookstore, contenttypes, sessions
    Running migrations:
    Applying bookstore.0003_auto_20210807_1002... OK

    mysql> show tables;
    +----------------------------+
    | Tables_in_mysite2 |
    +----------------------------+
    | auth_group |
    | auth_group_permissions |
    | auth_permission |
    | auth_user |
    | auth_user_groups |
    | auth_user_user_permissions |
    | book |
    | bookstore_author |
    | django_admin_log |
    | django_content_type |
    | django_migrations |
    | django_session |
    +----------------------------+
    12 rows in set (0.00 sec)

    ····
    image

    from django.db import models
    
    # Create your models here.
    class Book(models.Model):
        title=models.CharField('书名',max_length=50,default='',unique=True)
        pub=models.CharField('出版社',max_length=100,null=False,default='')
        price=models.DecimalField('图书价格',max_digits=7,decimal_places=2)
        market_price=models.DecimalField('图书零售价',max_digits=7,decimal_places=2,default=0.0)
        class Meta:
            db_table='book'
    
    
    class Author(models.Model):
        name=models.CharField('姓名',max_length=11)
        age=models.IntegerField('年龄',default=1)
        email=models.EmailField('邮箱',null=True)
        class Meta:
            db_table='author'
    

    author表中实际上age存在默认值只是在显示中并不直接表示在数据库中,实现解耦

    mysql> desc author;
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | int          | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(11)  | NO   |     | NULL    |                |
    | age   | int          | NO   |     | NULL    |                |
    | email | varchar(254) | YES  |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+
    4 rows in set (0.00 sec)
    
  • 相关阅读:
    Delphi以及三方控件的源代码规模
    InitInheritedComponent的执行过程
    poj 3897 Maze Stretching 二分+A*搜索
    一些窗口API函数,比如SetForegroundWindow,SwitchToThisWindow
    终于懂了:WM_PAINT 与 WM_ERASEBKGND(三种情况:用户操作,UpdateWindow,InvalidateRect产生的效果并不相同),并且用Delphi代码验证 good
    窗口绘制有关的消息整理 WM_PAINT, WM_NCPAINT, WM_ERASEBKGND
    WM_PAINT与WM_ERASEBKGND(用户操作和API这两种情况产生消息的顺序有所不同)
    关于WM_ERASEBKGND和WM_PAINT的深刻理解
    offsetHeight在OnLoad中为0的现象
    TWinControl.WMNCPaint对非客户的绘制
  • 原文地址:https://www.cnblogs.com/yescarf/p/15108264.html
Copyright © 2011-2022 走看看