由于现在加载应用程序的方式,以下显然在Django 1.7中不起作用:
entity = models.ForeignKey(Counterparty, default=Counterparty.objects.get(counterparty_name='A Company Ltd').pk, related_name='entity')
它会抛出django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
错误。有没有办法在1.7中实现同样的目的?
答案 0 :(得分:1)
似乎懒惰评估的诀窍,只是让fetch在运行时调用的匿名函数似乎可以解决这个问题
entity = models.ForeignKey(Counterparty, default=lambda: Counterparty.objects.get(counterparty_name='A Company Ltd').pk, related_name='entity')
谢谢schneck的提示!