我在“ external_app” 应用中具有这样的模型:“城市” :
class Cities(models.Model):
CityName = models.CharField(max_length=50, blank=False, db_index=True)
slug = models.SlugField(max_length=50, db_index=True, unique=True)
我有第二个应用,“地址程序” ,型号很少:
class CityRegions(models.Model):
city = models.ForeignKey('external_app.Cities', default=0, verbose_name=' City', related_name='City_for_region')
city_region = models.CharField(max_length=200, blank=False, default='', verbose_name='City region', unique=True)
city_region_slug = models.SlugField(verbose_name='City region slug')
和区域中子区域的第二个模型。现在,我要在CityRegions模型中发布二手城市。我尝试为城市字段添加FK
class RegionSquares(models.Model):
city = models.ForeignKey(CityRegions, default=0, verbose_name='City', related_name='City_for_regionsquare', to_field='city')
region = models.ForeignKey(CityRegions, default=0, verbose_name='City region',related_name='Region')
region_square = models.CharField(max_length=200, blank=False, default='', verbose_name='City sub-region')
region_square_slug = models.SlugField(verbose_name='City sub-region slug')
当我尝试进行迁移时,出现错误消息”(“ fields.E311”'CityRegions.city'必须设置为unique = True,因为它是由外键引用的。”
当我在CityRegions.City字段中设置unique = True并尝试迁移时,出现以下错误消息:“ django.db.utils.IntegrityError:唯一约束失败...”
问题是如何在 RegionSquares 模型中发布 CityRegions 模型城市中使用过的列表?
在常见应用中,应具有以下几种型号: -具有当前城市的城市区域的编辑列表(应显示区域和城市) -每个城市区域的可编辑子需求列表(应显示城市,区域和子区域) -街道的可编辑列表(应显示城市,区域和分区)
方案:
**cities** (model. Used in another apps of the project)
|-->**citie's regions** (parent for sub-regions) (city and region should me show in admin)
|--> **region's sub-regions** (parent for streets) (city,region and sub-region should me show in admin)
|--> **list of streets** (city,region, sub-region and street should me show in admin)
样本数据:
**Cities:** Moscow, Novosibirsk, Kemerovo
**Regions (city is parent):** Moscow - SVAO, Moscow - TSAO, Novosibirsk - Centralnij, Kemerovo - Jugniy
**Sub regions (City and region are parents):** Moscow - SVAO - sub-region A; Moscow - SVAO - subregion B; Moscow - SVAO sub-region C. Novosibirsk - Centralnij - sub-region A. Novosibirsk - Centralnij - sub-region B
**Streets (City, region and sub-region are parents):** Moscow - SVAO - sub-region A - street A; Moscow - SVAO - sub-region A - street B; Moscow - SVAO - sub-region A - street C; Novosibirsk - Centralnij - sub-region B -s treet A; Novosibirsk - Centralnij - sub-region B street B... and so on
所需结果:
Moscow
Moscow - SVAO
Moscow - SVAO - sub-region A
Moscow - SVAO - sub-region A - street A
Moscow - SVAO - sub-region A - street B
Moscow - SVAO - sub-region A - street C
Moscow - SVAO - sub-region B
Moscow - SVAO - sub-region B - street A
Moscow - SVAO - sub-region B - street B
Moscow - SVAO - sub-region B - street C
Moscow - SVAO - sub-region C
Moscow - SVAO - sub-region C - street A
Moscow - SVAO - sub-region C - street B
Moscow - SVAO - sub-region C - street C
Novosibirsk
Novosibirsk - Centralnij
Novosibirsk - Centralnij - sub-region A - street A
Novosibirsk - Centralnij - sub-region A - street B
Novosibirsk - Centralnij - sub-region A - street C
and so on...
我使用 Django 1.11 和python 2.7.10
答案 0 :(得分:0)
迁移失败的原因可能是您有多个CityRegions
和同一city
答案 1 :(得分:0)
city
不是CityRegions的PK(主键),因此您不应引用它。唯一性失败,因为已经有重复的值-很好。
要使其正常工作:
a)标准化您的数据模型,并从city
中删除RegionSquares
字段-必要时通过region
实现。像CityRegions.objects.filter(region__city__CityName='Moscow')
或
b)参考City
模型本身:
class RegionSquares(models.Model):
city = models.ForeignKey('external_app.Cities', default=0, verbose_name=' City', related_name='City_for_region')
...
等于CityRegions
此字段的定义。
同样,如果您的问题仍然是关于如何建模证明的关系(并摆脱问题中提到的所有错误),那么答案已经在这里-删除city
RegionSquares
模型中的字段。您已经有了指向region
的链接,该链接(区域记录)包含city
的值。这是上述选项a
。