gregmuellegger/django-autofixture

Control over related models

Closed this issue · 1 comments

So i have a model called Usage, with a many to many to another model called Reservation and this reservation has a foreign key to another named Offer

How can i create test data with the fk and m2m automatically generating the new objects?

this is what i have so far:

from autofixture import AutoFixture
from .models import Usage


def run(verbose=True):
    fixture = AutoFixture(
        Usage, generate_fk=['card', 'reservations__offer'],
        generate_m2m={'reservations': (1, 1)}
    )
    user_card_usages = fixture.create(2)
    print ("Finished loading fixtures: ", user_card_usages)

The reservations__offer is not being generated (i am not sure about the syntax)

I think we cannot do this at the moment with the current implementation.
I would recommend creating the Usage instances one by one and then creating a new Reservation object for every Usage object, like this:

fixture = AutoFixture(Usage, generate_fk=['card'])
user_card_usages = []
for i in range(2):
    usage = fixture.create_one()
    usage.reservations.add(
        autofixture.create_one(Reservation, generate_fk=['offer']))
    user_card_usages.append(usage)
print ("Finished loading fixtures: ", user_card_usages)