When using using mommy.make() directly on a model class, I can override related (foreign key) fields no problem.
But when I create a recipe for the same model, and I don't use field=foreign_key(other_recipe), it doesn't work and the value I specify is ignored.
>>> from app1 import models as myapp
>>> from app2 import models as otherapp
>>> from model_mommy import mommy, recipe
>>> InviteResponse = recipe.Recipe('myapp.InviteResponse')
# Doesn't use the field value I pass in.
>>> ir = InviteResponse.make(subscriber__list=otherapp.List.objects.create(name='test-name-1'))
>>> ir.subscriber.list
<List: 2-381cbce7ae9; MpTZOopHRvPARKPxhWHbafQfjvpktCNFCrkGcPJlCOvfLAmiYj>
# Does use the field value I pass in.
>>> ir = mommy.make(myapp.InviteResponse, subscriber__list=otherapp.List.objects.create(name='test-name-2'))
>>> ir.subscriber.list
<List: 3-96f2324c273; test-name-2>
# If I use foreign_key() in the recipe and create a dummy recipe for the related model, it works.
>>> Subscriber = recipe.Recipe('otherapp.Subscriber')
>>> InviteResponse = recipe.Recipe('myapp.InviteResponse', subscriber=recipe.foreign_key(Subscriber))
>>> ir = InviteResponse.make(subscriber__list=otherapp.List.objects.create(name='test-name-3'))
>>> ir.subscriber.list
<List: 4-82e9b30d0e3; test-name-3>
Now it's not that hard to create a recipe for the related field and use foreign_key(), but I shouldn't have to. If it works for models it should work for recipes. In this case app2 is a 3rd party app and I don't want to create empty recipes for 3rd party models when I don't care about their data, just so I can override related fields when creating objects in my app.
When using using
mommy.make()
directly on a model class, I can override related (foreign key) fields no problem.But when I create a recipe for the same model, and I don't use
field=foreign_key(other_recipe)
, it doesn't work and the value I specify is ignored.Now it's not that hard to create a recipe for the related field and use
foreign_key()
, but I shouldn't have to. If it works for models it should work for recipes. In this caseapp2
is a 3rd party app and I don't want to create empty recipes for 3rd party models when I don't care about their data, just so I can override related fields when creating objects in my app.