python - FactoryBoy assign attribute to attribute of SubFactory -
in defining modelfactory in factoryboy, need access attribute of model created subfactory , assign modelfactory's attribute.
this want do:
import factory class mymodelfactory(factory.djangomodelfactory):     factory_for = mymodel      created_by = factory.subfactory(adminuserfactory)**.id** obviously doesn't work because there no adminuser object access id in mymodelfactory class definition.
this have done, ugly:
import factory class mymodelfactory(factory.djangomodelfactory):     factory_for = mymodel      dummy_created_by = factory.subfactory(adminuserfactory)     created_by = factory.lazyattribute(lambda o: o.dummy_created_by.id)      @classmethod     def _create(cls, target_class, *args, **kwargs):         del kwargs['dummy_created_by']         return super(mymodelfactory, cls)._created(             target_class, *args, **kwargs) i trying read through factory_boy docs didn't see class or function allow me lazily access attribute. have suggestions?
use selfattribute:
class mymodelfactory(factory.django.djangomodelfactory):     factory_for = mymodel     dummy_created_by = factory.subfactory(adminuserfactory)     created_by = factory.selfattribute('dummy_created_by.id') 
Comments
Post a Comment