berinhard / model_mommy

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

Add settings.MOMMY_CUSTOM_RECIPES dict so that certain models can be automatically created from recipes #122

Closed tkrajca closed 11 years ago

tkrajca commented 11 years ago

In our scenario we have a model Prescription that cannot be generated by mommy (for whatever reason we just need our special Prescription). We've got a recipe to create instances of Prescription. Also, we have got lots of other models that have foreign keys to prescription, we want these models to be generated by mommy.

So, I would like to do something like:

MOMMY_CUSTOM_RECIPES = { 'Prescription': prescription_recipe }

to get mommy to do prescription_recipe.make() whenever mommy.make() is called on the prescription model (as instance, related instance, many_to_many instance, ... always).

tkrajca commented 11 years ago

Pull request comming soon ...

tkrajca commented 11 years ago

mommy.make or mommy.prepare, off course.

tkrajca commented 11 years ago

I guess this is sort of an alternative to make_recipe and prepare_recipe.

vandersonmota commented 11 years ago

Why not create the other models recipes just with the foreign_key declaration to Prescription model?

or you can create this function, for instance:

def mymake(model, **kwargs):
    if not 'prescription' in kwargs:
        kwargs.update({'prescription': mommy.make_recipe('myapp.prescription')})
    return mommy.make(model, **kwargs)
tkrajca commented 11 years ago

Ok, but if I don't want to create a recipe for all my models?

We have got lots of models, the only one that we want to have a recipe for is Prescription because it does some magic in .save(). The rest of the models have a foreign key to Prescription but if we use mommy to generate the original model, it will fail because it will try to generate the Prescription without the recipe.

If we write recipes for the rest of our models, then we might as well use fixtures.

We actually ended up "overriding" the make and prepare methods (similarly to what you suggested in the previous comment) and then putting that make/prepare into MOMMY_CUSTOM_FIELDS_GEN for foreign key, one to one field and many to many fields but that seems a little bit complicated for such a simple thing, I think.

vandersonmota commented 11 years ago

It seems a very specific case and i don't think it's worth to handle it directly on model_mommy's api, at least for now.

But thanks anyway!