在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"
迁移时抛出此错误 有什么想法?。
答案 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()