berinhard / model_mommy

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

Allow to extend existent recipes #178

Closed baitcode closed 10 years ago

baitcode commented 10 years ago

I've got simple_booking recipe and I want to make complex_booking recipe by simple override of few fields, currently, I have to do:

simple_booking = Recipe(Booking,
  name = 'Ilya', price=100,
)
complex_booking = Recipe(Booking,
  name = 'Ilya', price=100, does_complex_stuff = True
)

preferred syntax would be:

simple_booking = Recipe(Booking,
  name = 'Ilya', price=100,
)
complex_booking = simple_booking.extend(
  does_complex_stuff = True
)

So I monkey patched mommy with:

# Monkey patch
def extend(self, **attrs):
    attr_mapping = self.attr_mapping.copy()
    attr_mapping.update(attrs)
    return Recipe(self.model, **attr_mapping)

Recipe.extend = extend

But monkey patching is inconvenient. Any chance that you'll implement something like this? Or you want to me make pull request with that code?

vandersonmota commented 10 years ago

Indeed. I rather have an extend method in the Recipe class. If you're going to send the PR, don't forget to add tests and docs.

thanks