Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
RelatedFactory created even when passing None kwargs #62
There is no way to avoid the creation of an object via RelatedFactory even when this related param is set to None via kwargs in factory instantiation. Example:
** Factory file **
** Test file **
obj = MyFactory(param1=None) # param1 exists in db
I'm facing the same issue... And I'm currently having to resort to some weird stuff using
for name, decl in sorted(postgen_declarations.items()): extracted, extracted_kwargs = postgen_attributes[name] # If I passed any value for a RelatedFactory, don't call the RelatedFactory if extracted is not None and isinstance(decl, RelatedFactory): continue decl.call(obj, create, extracted, **extracted_kwargs)
But considering that we could want to send an object instead of asking the factory not to create one, maybe the check should be in the
def call(self, obj, create, extracted=None, **kwargs): passed_kwargs = dict(self.defaults) passed_kwargs.update(kwargs) if self.name: passed_kwargs[self.name] = obj # If we passed an object of type I'm supposed to create, just set the attribute and save if we have to if isinstance(extracted, self.get_factory().FACTORY_FOR): setattr(extracted, self.name, obj) if create: extracted.save() # If we passed no other value, then we can generate the object using the factory elif extracted is None: self.factory.simple_generate(create, **passed_kwargs)
I haven't tried this, so I don't know if it works, or if it breaks something else, but I do believe something like that is needed...
@Dhekke Yep, your analysis pretty much nailed the problem here :)
I'm conflicted about the best solution for this:
The simple in terms of API would be option 1, but I don't really like using a pseudo-None object.
What do you think?
Yeah, I'm not a fan of Option 1 for the same reason, it just seems a bit overkill.
As for the rest, I believe having
At the same time,
That leaves Option 2 but I don't really see how it would solve the issue =/
All of this is to say that I like Option 3 the most, and if it's paired with something like
for name, decl in sorted(postgen_declarations.items()): extracted, extracted_kwargs = postgen_attributes[name] # If I passed any value for a RelatedFactory, don't call the RelatedFactory if factory_extracted is False and isinstance(decl, RelatedFactory): continue decl.call(obj, create, extracted, **extracted_kwargs)
we don't even have to change method signatures.
I guess I'll go for Option 3, but I'll pass the
It still means that all postgen_declarations will suddenly begin receiving a