Let's say you have these Django models related this way:
class Service:
restaurant = models.ForeignKey(Restaurant)
hist_day_period = models.ForeignKey(DayPeriod)
class DayPeriod:
restaurant = models.ForeignKey(Restaurant)
I want to create a Service
object, using a Factory. It should create all 3 models but use the same restaurant.
Using this code:
class ServiceFactory(factory.django.DjangoModelFactory):
class Meta:
model = Service
restaurant = factory.SubFactory('restaurants.factories.RestaurantFactory')
hist_day_period = factory.SubFactory(
'day_periods.factories.DayPeriodFactory', restaurant=restaurant)
Factory boy will create 2 different restaurants:
s1 = ServiceFactory.create()
s1.restaurant == s1.hist_day_period.restaurant
>>> False
Any idea on how to do this? It's unclear to me if I should use related factors
instead of SubFactory
to accomplish this.