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)
原文地址:https://www.cnblogs.com/yescarf/p/15108264.html