简体   繁体   English

Django 1.7中ORM对象值作为模型字段的默认值

[英]ORM object value as default for model field in Django 1.7

The following obviously doesn't work in Django 1.7 due to the way apps are loaded now: 由于现在加载应用程序的方式,以下内容显然在Django 1.7中不起作用:

entity = models.ForeignKey(Counterparty, default=Counterparty.objects.get(counterparty_name='A Company Ltd').pk, related_name='entity')

It throws a django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. 它抛出django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. error.. Is there a way to achieve the same thing in 1.7? 错误..是否有办法在1.7中实现相同的目的?

Seems lazy evaluation does the trick, simply making the fetch an anonymous function that is called at runtime appears to do the trick 似乎懒惰的评估可以解决问题,只是使获取时在运行时调用的匿名函数看起来很有效

entity = models.ForeignKey(Counterparty, default=lambda: Counterparty.objects.get(counterparty_name='A Company Ltd').pk, related_name='entity')

Thank you schneck for the tip! 谢谢schneck的小费!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM