1.抽象基类表
class BaseModel(models.Model):
create_time = models.DateTimeField(auto_now_add=True)
upload_time = models.DateTimeField(auto_now=True)
class Meta:
abstract = True #可继承
2.创建表
# 主键
id = models.AutoField(primary_key=True)
# 字符串
name = models.CharField(max_length=50)
#整形
number = models.IntegerField()
#小数
price = models.DecimalField(max_digits=5,decimal_places=2)
#IP(自带正则)
ip = models.GenericIPAddressField()
#图片(自带正则,可设置二级路径)
avator = models.ImageField(null=True,blank=True,upload_to='/图片地址')
#布尔,True and False
is_active = models.BooleanField(default=True)
3.外键
// 一对多
cid = models.ForeignKey(Cate,
on_delete='CASCADE' #关联删除,
on_delete=models.SET_NULL, #非关联删除
null=True, #可以为空
blank=True, #表单提交可以为空
)
many = models.ManyToMany(关联表的类名)