from django.db import models标签:建表,python,max,数据库,models,length,CharField,Model,class From: https://www.cnblogs.com/shclbear/p/16622319.html
# Create your models here.
class User(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
register_time = models.DateField() # 年月日
# def __str__(self):
return '对象:%s'%self.name #为了方便查看写一个
# register_time = models.DateTimeField() #有time 的有时分秒
'''
DateField
DateTimeField
两个重要参数
auto_now : 每次操作数据的时候,该字段会自动将当前的时间更新
auto_now_add : 在创建数据的时候会自动将当前创建的时间记录下来,之后只要不人为的修改,那么就一直不变 :
'''
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=8,decimal_places=2)
publish_date = models.DateField(auto_now_add=True)
# 一对多
publish = models.ForeignKey(to='Publish',on_delete=models.CASCADE)
# 多对多
authors = models.ManyToManyField(to='Author')
class Publish(models.Model):
name = models.CharField(max_length=32)
addr = models.CharField(max_length=64)
email = models.EmailField() # vatchar(254)
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
# 一对一
author_detail = models.OneToOneField(to = 'AuthorDetail',on_delete=models.CASCADE)
class AuthorDetail(models.Model):
phone = models.BigIntegerField()
addr = models.CharField(max_length=64)