-
字段类型
from django.db import models # Create your models here. # 自定义模型必须继承Model class User(models.Model): # 字段名:不能是python的关键字;不能使用连续的下划线; #db_column:数据库表中的字段名称 uid = models.AutoField(primary_key=True) # CharField类型必须指明长度max_length username = models.CharField(max_length=30,unique=True) password = models.CharField(max_length=128) regtime = models.DateTimeField(auto_now_add=True) class Meta: # 元数据,模型本身的信息 #默认表名:应用名_模型名 db_table = 'user' # 表名 ordering = ['username'] # 排序class User(models.Model): uid = models.AutoField(primary_key=True) username = models.CharField(max_length=30,null=False) password = models.CharField(max_length=128,null=False) regtime = models.DateTimeField(auto_now=True) sex = models.IntegerField() def __str__(self): return self.username+":"+ str(self.uid) class Meta: db_table = 'user' class Detail(models.Model): did = models.AutoField(primary_key=True) phone = models.CharField(max_length=30) uid = models.IntegerField() class Meta: db_table = 'detail'
Django—模型—字段类型
最新推荐文章于 2025-04-11 12:09:04 发布
本文深入探讨了Django中模型字段的使用,包括AutoField、CharField、DateTimeField和IntegerField等,讲解了如何定义主键、唯一性约束、自动填充时间戳及性别字段的实现方式。同时,介绍了元数据类Meta的使用,包括自定义表名和排序规则。
623

被折叠的 条评论
为什么被折叠?



