berinhard / model_mommy

No longer maintained, please migrate to model_bakery
http://model-bakery.readthedocs.org/
Other
903 stars 141 forks source link

Overriding values for related objects doesn't work in recipes that don't define foreign_key() #110

Closed mrmachine closed 11 years ago

mrmachine commented 11 years ago

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.

berinhard commented 11 years ago

Nice one @mrmachine! I'm a +1 for this fix.

vandersonmota commented 11 years ago

Fixed: b13e0ef5ed2946500c7b2a345e8c00ecd2521b56 ;-)

Thanks for the report @mrmachine