multiple recipes with common foreign_keys

44 views
Skip to first unread message

Jacob G

unread,
Nov 27, 2015, 11:21:59 AM11/27/15
to Model Mommy
If I have recipes like this:
org = Recipe(
    'my.Organization',
    active=True
)

org_unit = Recipe(
    'my.OrganizationUnit',
    organization=foreign_key(org),
    active=True
)

dept = Recipe(
    'my.Department',
    organization=foreign_key(org),
    active=True
)

employee = Recipe(
    'my.Employee',
    organization=foreign_key(org),
    org_unit=foreign_key(org_unit),
    department=foreign_key(dept),
    active=True
)

And I want to create an employee with an existing organization like this:

org = mommy.make_recipe('my.org') 
 # ... use org to create some other models which have nothing to do with employee ....
mommy.make_recipe('my.employee', organization=org)

I would like the org passed to the Employee recipe to be automagically passed into the creation of org_unit and dept. But in reality, they're not. Is there a different way to accomplish this. Otherwise, I'd have to basically make every receipt explicitly.

vanders...@gmail.com

unread,
Nov 28, 2015, 2:11:18 PM11/28/15
to Jacob G, Model Mommy
maybe 
mommy.make_recipe('my.employee', organization=org, org_unit__organization=org, department__organization=org)
is what you're looking for.

[]'s

--
You received this message because you are subscribed to the Google Groups "Model Mommy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to model-mommy...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Vanderson Mota dos Santos

Jacob G

unread,
Nov 28, 2015, 6:28:46 PM11/28/15
to Model Mommy, ja...@fareclock.com
Thanks, I mean a way to declare it in the recipe. For example, in factory_boy I can do this:

class EmployeeFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Employee

first_name = u'Fédérée'
last_name = u'Employee'
active = True

organization = factory.SubFactory(OrganizationFactory)
org_unit = factory.SubFactory(OrganizationUnitFactory, organization=factory.SelfAttribute('..organization'))
department = factory.SubFactory(DepartmentFactory, organization=factory.SelfAttribute('..organization'))

vanders...@gmail.com

unread,
Nov 28, 2015, 6:55:21 PM11/28/15
to Jacob G, Model Mommy
ok, i guess you can use the "related" function, in order to use other related recipes

Jacob G

unread,
Nov 28, 2015, 7:02:19 PM11/28/15
to Model Mommy, ja...@fareclock.com
Is this supported?

dept = Recipe(
    'my.Department',
    organization=foreign_key(org),
    active=True
)

employee = Recipe(
    'my.Employee',
    organization_set=related(dept_org)
)

i.e., in my scenario, I don't just want to share the same parent. I want to share the foreign key of a parent.

vanders...@gmail.com

unread,
Nov 28, 2015, 7:12:05 PM11/28/15
to Jacob G, Model Mommy
Currently, you can point to the same recipe, using the "related" function. 
But it will not point to the same registry used by other model (i think you meant this, if i understood correctly). It will just create other with the same parameters.


[]'s
Reply all
Reply to author
Forward
0 new messages