Django model更改继承的类时需要的默认值

Shop原来是继承models.Model的,现在继承BaseModel

class BaseModel(models.Model):
    """
    基类
    """
    is_valid = models.BooleanField(default=True, verbose_name=u'有效性')
    created = models.DateTimeField(auto_now_add=True, verbose_name=u'创建时间')
    modified = models.DateTimeField(auto_now=True, verbose_name=u'修改时间')

    class Meta:
        abstract = True

    def valid_action(self, valid=True):
        self.isvalid = valid
        self.save()


class Shop(BaseModel):
    shopId = models.AutoField(u'商店编号', primary_key=True)
    shopName = models.CharField(u'商店名称', max_length=50)
    shopUrl = models.CharField(u'商店网址', max_length=100)

    # def to_json_string(self):
    #     return json.dumps(self.__to_dict__(), ensure_ascii=False).decode('utf-8').encode('gb2312')

    def to_dict(self):
        return {
            'shop_id': self.shopId,
            'shop_name': self.shopName,
            'shop_url': self.shopUrl,
        }

    def __unicode__(self):
        return 'shopId=' + str(self.shopId) + ' shopName=' + self.shopName

如果不写

class Meta:
        abstract = True
在makemigration时会出现


加上

class Meta:
        abstract = True
将基类设置为抽象类,就不会要求填写默认值了

你可能感兴趣的:(django)