abilic
abilic

Reputation: 81

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:

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. error.. Is there a way to achieve the same thing in 1.7?

Upvotes: 1

Views: 166

Answers (1)

abilic
abilic

Reputation: 81

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!

Upvotes: 1

Related Questions