Creating multiple objects with foreign key
Asked Answered
P

4

15

I need to create ten sample users (User) and each of them must have fifty documents (Doc). How to do this in tests.py using factoryboy?

#factories.py

from app_name.models import *
import factory
from datetime import datetime, timedelta, time
from django.contrib.auth.models import User


class UserFactory(factory.Factory):
    FACTORY_FOR = User

    username = factory.Sequence(lambda n: 'User ' + n)
    email = '[email protected]'
    password = '1234567'

class DocFactory(factory.Factory):
    FACTORY_FOR = Doc

    user = factory.SubFactory(UserFactory)
    kategories = '1'
    doc_number = '12345678'
    date_join = factory.Sequence(lambda n:(datetime.now() + timedelta(days=n)).date(), int)

in my tests.py:

from django.test import TestCase
from django_dynamic_fixture import G
from factories import *
Pepita answered 29/1, 2013 at 18:7 Comment(0)
W
17
users = UserFactory.create_batch(10)
for user in users:
    doc = DocFactory.create(user=user)
Walter answered 29/1, 2013 at 22:48 Comment(0)
P
8

You can use a post_generation decorator:

class UserFactory(factory.Factory):

    ...

    @factory.post_generation
    def create_docs(self, create, extracted, **kwargs):
        if not create:
            return
        for i in range(50):
            doc = DocFactory.create(user=self)
Polycarp answered 12/8, 2014 at 14:31 Comment(0)
L
1

You can simply do batch_create so that for each DocFactory object new UserFactory object will create

`DocFactory.create_batch(10)`
Laynelayney answered 23/9, 2021 at 9:54 Comment(0)
G
0

For those of you working with SQLAlchemy, this can be done with the following recipe (notice that I'm using the Person/Address models instead of the User/Docs model example above).

from sqlalchemy import create_engine, Integer, Text, ForeignKey, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, scoped_session, sessionmaker

import factory
from factory.alchemy import SQLAlchemyModelFactory as sqla_factory
import random

engine = create_engine("sqlite:////tmp/factory_boy.sql")
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()

class Person(Base):
    id = Column(Integer, primary_key=True)
    name = Column(Text)
    addresses = relationship("Address", backref="person")

class Address(Base):
    id = Column(Integer, primary_key=True)
    city = Column(Text)
    street = Column(Text)
    street_number = Column(Integer)
    person_id = Column(Integer, ForeignKey('person.id'))

class AddressFactory(sqla_factory):
    class Meta:
        model = Address
        sqlalchemy_session = session
    street_number = random.randint(0, 10000)
    street = "Commonwealth Ave"
    city = "Boston"

class PersonFactory(sqla_factory):
    class Meta:
        model = Person
        sqlalchemy_session = session
    name = "John Doe"

Base.metadata.create_all(engine)
for i in range(100):
    person = PersonFactory(addresses=AddressFactory.create_batch(3))

This creates 3 workouts for each person created, where each workout references the person via the person_id FK.

Groundage answered 9/5, 2017 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.