I'm building a factory with factory_boy
that generates a django model. I would like to see what arguments the user inputs inline. My factory itself looks like this
class SomeFactory(factory.django.DjangoModelFactory):
name = factory.Sequence(lambda n: 'Instance #{}'.format(n))
some_other_thing = factory.SubFactory(SomeOtherFactory)
class Meta:
model = SomeModel
Now the user could say s = SomeFactory()
and it would work fine, but I want to detect if the user input their own argument. For instance, to tell if the user passed in their own name, as in s = SomeFactory(name='Matt')
What I've tried so far is
- Writing my own
__init__
function in theSomeFactory
class- This gets mysteriously overwritten and is neither called when I call
s = SomeFactory()
, nor when I calls.__init__()
- This gets mysteriously overwritten and is neither called when I call
- Same goes for overwriting the
__new__
method - Overwriting the poorly named
_adjust_kwargs
- This gives me all fields as
kwargs
, not just the ones the user defined. For instance, callings = SomeFactory(name='Matt')
, I would get akwargs
dict with keys forname
andsome_other_thing
, which makes it impossible to tell input their own argument or not
- This gives me all fields as
- Overwriting
_create
- Still encounter the same problem with overwriting
_adjust_kwargs
, in thatkwargs
doesn't contain the originalkwargs
, but rather all of the arguments
- Still encounter the same problem with overwriting
I think a lot of the functionality I'm after is black-boxed inside of factory_boy
's StepBuilder
(I suspect it's in the instantiate
method) but I have no idea how to modify it to do what I want.
Does anyone have any thoughts on how to figure out which kwargs
were set originally in the call to s = SomeFactory()
? I.e. determine that if I said s = SomeFactory(name='Matt')
, that the user manually set the name?
Thanks!
Update: I'm running django
version 1.11.2
, factory_boy
version 2.8.1
, and python
version 3.5.2