django--orm--01

常见问题处理一

报错信息:myapp.Article.tags: (fields.W340) null has no effect on ManyToManyField.

解决方法:tags = models.ManyToManyField("Tag") 不用跟null

常见问题处理二

报错信息:ERRORS:
myapp.Tag.date: (fields.E130) DecimalFields must define a 'decimal_places' attribute.
myapp.Tag.date: (fields.E132) DecimalFields must define a 'max_digits' attribute.

解决方法:DecimalFields字段的“decimal_places(有几位小数) 和 “max_digits(总共有几位数)”

class Account(models.Model):
    """账户表"""
    username = models.CharField(max_length=64,unique=True)
    password = models.CharField(max_length=255)
    email = models.EmailField(unique=True)
    register_date = models.DateTimeField(auto_now_add=True) # 注册时间auto_now_add 当前时间,不能被重写
    signature = models.CharField("签名",max_length=255,null=True)

class Article(models.Model):
    """文章表"""
    title = models.CharField(max_length=255,unique=True)
    context = models.TextField()
    account = models.ForeignKey("Account",on_delete=models.CASCADE) #  models.CASCADE 当用户删除时,文章也删除
    tags = models.ManyToManyField("Tag")
    pub_date = models.DateTimeField()

class  Tag(models.Model):
    "标签表"
    name = models.CharField(max_length=64,unique=True)
    date = models.DateTimeField(auto_now_add=True)

猜你喜欢

转载自www.cnblogs.com/ljf520hj/p/11716940.html