1

在 django 中创建迁移时出现错误

模型.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"

在迁移时抛出此错误任何想法?

迁移错误

4

1 回答 1

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()
于 2016-07-09T00:10:06.320 回答