首页 > 其他 > 详细

django-orm基础字段及选项1

时间:2021-08-07 15:01:28      阅读:44      评论:0      收藏:0      [点我收藏+]

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有一定区别
技术分享图片
技术分享图片
技术分享图片

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

针对模型类表的操作

meta类
技术分享图片

# 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:\Users\Administrator\Desktop\mysite2>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, bookstore, contenttypes, sessions
Running migrations:
Applying bookstore.0002_author... OK

C:\Users\Administrator\Desktop\mysite2>python manage.py makemigrations
Migrations for ‘bookstore‘:
bookstore\migrations\0003_auto_20210807_1002.py
- Rename table for book to book

C:\Users\Administrator\Desktop\mysite2>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)

····
技术分享图片

django-orm基础字段及选项1

原文:https://www.cnblogs.com/yescarf/p/15108264.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!