错误models.py Django

时间:2016-07-08 22:35:34

标签: python django migrate makemigrations

在django

中创建迁移时出错

models.py

class StateBuyers(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "states"

class CountiesBuyers (models.Model):
    state = models.ForeignKey(StateBuyers, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "counties"


class CityBuyers (models.Model):
    state = models.ForeignKey(StateBuyers, on_delete=models.CASCADE)
    county = models.ForeignKey(CountiesBuyers, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "cities"

也许在这里我有错误 创建一个多个实例

class Buyers(models.Model):
    name = models.CharField(max_length= 50)
    state = models.ForeignKey(StateBuyers, related_name="Buyers", null=True, on_delete=models.CASCADE)
    city = models.ForeignKey(CityBuyers, related_name="Buyers", null=True, on_delete=models.CASCADE)
    phonenumber = models.CharField(max_length=15)
    email = models.EmailField (max_length= 50)
    notes = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "buyers"

迁移时抛出此错误 有什么想法?。

migration error

1 个答案:

答案 0 :(得分:1)

稍微偏离主题,但为什么不遵守惯例?

  • 使用单数形式的简单模型名称
  • 如果与默认值相同,则无需额外参数
  • 如果有县的国家,为什么重复国家?任何业绩增长?
class NameAsReprMixin(object):
    def __unicode__(self):
        return self.name

class State(models.Model, NameAsReprMixin):
    name = models.CharField(max_length=100)    


class County(models.Model, NameAsReprMixin):
    state = models.ForeignKey(State)
    name = models.CharField(max_length=100)


class City(models.Model, NameAsReprMixin):
    county = models.ForeignKey(County)
    name = models.CharField(max_length=100)


class Buyer(models.Model, NameAsReprMixin):
    name = models.CharField(max_length= 50)
    state = models.ForeignKey(State, related_name="buyers", null=True)
    city = models.ForeignKey(City, related_name="buyers", null=True)
    phone_number = models.CharField(max_length=15)
    email = models.EmailField(max_length= 50)
    notes = models.CharField(max_length=200)

然后:

# all city buyers
City.object.get(...).buyers.all()

# all state byuers
State.object.get(...).buyers.all()