Open riconnon opened 5 years ago
This is indeed a longstanding, complex issue (see #69 !).
There aren't any patterns yet, this would need to change the core builder to perform the following steps:
get_or_create
fields"get_or_create
Any help would be very welcome!
I have managed to do something similar (I'm not modifying the returned value) but fetching an existing object based on a parameter passed to the factory and sharing here in case it is useful to others. Here if default_type
is passed into the factory then we fetch an object from the database instead of creating one by calling it's factory)
class ActivityFactory(DjangoModelFactory):
class Meta:
model = Activity
class Params:
default_type = None
date = FuzzyDateTime(start_dt=datetime(2010, 1, 1, tzinfo=pytz.utc), end_dt=now(), force_microsecond=0)
@factory.lazy_attribute
def type(self):
if self.default_type:
return ActivityType.objects.get(default_type=self.default_type)
return ActivityTypeFactory()
Description
I have a scenario where I need to, based on some condition, ensure that a factory returns an existing object (possibly customising it to match the provided fields) rather than creating a new object. I tried overriding _create to do this but it seems like subfactories have already been called before this point.
I'm not sure if there's an existing extension point where I can sensible make this choice or if changes to factory_boy itself would be needed
Model / Factory code
The issue
Using the above code the behaviour is fine, except that the SubFactory for OtherModel gets called even in the case of "condition"
Notes
I'd like to know if there's an existing pattern for this case in factory_boy or whether you'd be open to accepting a change to support it.