You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some Django Model Factories defined as such:
class TripFactory(DjangoModelFactory):
class Meta:
model = Trip
class Params:
operator = factory.SubFactory(OperatorFactory)
trip_id = "100"
operator_service_day = factory.SubFactory(
OperatorServiceDayFactory, operator=factory.SelfAttribute('operator')
)
and the OperatorServiceDayFactory:
class OperatorServiceDayFactory(DjangoModelFactory):
class Meta:
model = OperatorServiceDay
class Params:
operator = factory.SubFactory(OperatorFactory)
operator = factory.SelfAttribute("operator")
What I would like to do is call an operator instance from my DB and pass it through to the Factories:
raise errors.CyclicDefinitionError(
factory.errors.CyclicDefinitionError: Cyclic lazy attribute definition for 'operator'; cycle found in ['operator'].
Proposed solution
It isn't clear from the Params documentation on how to pass an object to the Params and use it instead of the SubFactory declaration.
I'm not sure how to proceed and I just need an Operator object passed down to my nested Factories.
The text was updated successfully, but these errors were encountered:
alxvallejo
changed the title
Passing SubFactory as Param results in Cyclic lazy attribute definition error
Passing object as Param results in Cyclic lazy attribute definition error
Mar 7, 2024
In TripFactory, when you write factory.SubFactory(operator=factory.SelfAttribute("operator")), it translates to OperatorServiceDayFactory.operator = OperatorServiceDayFactory.operator — that's a cyclical one. Instead, use factory.SelfAttribute("..operator") to fetch the value passed to TripFactory
Similarly, in OperatorServiceDayFactory, the operator = factory.SelfAttribute("operator") creates a loop. The class Params is intended for keyword arguments that will be used to define the other fields of the instance, but should not be passed to the instance. If the parameter is a field of the model, just declare it in the main declarations — no need to put it in class Params.
Here is how I would declare those factories:
classTripFactory(DjangoModelFactory):
classMeta:
model=Trip# If `Trip` has an `operator` field:operator=factory.SubFactory(OperatorFactory)
# If `Trip` doesn't have an `operator` field, but you want to be able to# call `TripFactory(operator=my_operator)`# instead of `TripFactory(operator_service_day__operator=my_operator)`:classParams:
operator=factory.SubFactory(OperatorFactory)
trip_id="100"operator_service_day=factory.SubFactory(
OperatorServiceDayFactory,
# Forward TripFactory.operator to OperatorServiceDayFactory.operatoroperator=factory.SelfAttribute('..operator'),
)
classOperatorServiceDayFactory(DjangoModelFactory):
classMeta:
model=OperatorServiceDay# Create an operator, unless one is explictly provided when calling the factoryoperator=factory.SubFactory(OperatorFactory)
The problem
I have some Django Model Factories defined as such:
and the
OperatorServiceDayFactory
:What I would like to do is call an
operator
instance from my DB and pass it through to the Factories:This results in:
Proposed solution
It isn't clear from the
Params
documentation on how to pass an object to the Params and use it instead of theSubFactory
declaration.I'm not sure how to proceed and I just need an Operator object passed down to my nested Factories.
The text was updated successfully, but these errors were encountered: