运行数据迁移时出错:
Running migrations:
Applying bird_monitoring.0027_auto_20150327_1102...Traceback (most recent call last):
File "/home/john/.virtualenvs/Client/lib/python3.4/site-packages/django/db/models/fields/related.py", line 555, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Locality' object has no attribute '_owner_cache'
During handling of the above...
“位置”具有属性所有者:
owner = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='Owner', blank=True, null=True, on_delete=models.SET_NULL)
配置:Ubuntu,Python 3.4,Django 1.7.2,SQLite
感谢您的帮助!
更新: “目标”模式:
class CampaignRegistration(models.Model):
campaign = models.ForeignKey(Campaign, verbose_name='campaign')
scrutineer = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='scrutineer')
locality = models.ForeignKey(Locality, verbose_name='locality')
迁移代码:
def transfer_become_scrutineers(apps, schema_editor):
Campaign = apps.get_model("app", "Campaign")
Locality = apps.get_model("app", "Locality")
CampaignRegistration = apps.get_model("app", "CampaignRegistration")
campaign = Campaign.objects.get(pk=1)
for locality in Locality.objects.all():
if locality.owner is None:
pass
else:
registration = CampaignRegistration()
...
registration.save()