paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
390 stars 67 forks source link

`get_or_create` and `update_or_create` support? #29

Closed mrmachine closed 4 years ago

mrmachine commented 11 years ago

I'd like to be able to specify some unique combination of fields to G (or F) and have DDF try to get the matching object and either update or create... I imagine it working like this:

# get by `a` and `b`, or create with `a`, `b` and `c`.
G(a='abc', b='def', c='ghi', _get=['a', 'b'])

# get by `a` and `b` and update `c`, or create with `a`, `b` and `c`.
G(a='abc', b='def', c='ghi', _update=['a', 'b'])

I want to use DDF with a custom management function to quickly populate test data to play with interactively (not in a test case).

Using the default sequential data fixture I quickly run into integrity errors. Using the random data fixture I might avoid them briefly, but still quickly run into problems.

Adding get_or_create and update_or_create support I could safely re-run my management command any time to create missing or update existing test data.

paulocheque commented 11 years ago

This sounds to be an interesting feature. I am out of time to implement this right now, but if you implement this with automated tests and make a pull request I will merge it.

It appears to me this can have a very isolated implementation with just a small change in the G/F functions that will read this parameters _get and _update.

paulocheque commented 4 years ago

I was thinking about that, I guess the simplest solution to populate a DB is using a random fixture. It could have some integrity error eventually, but it shouldn't be very critical since these data should be for testing purposes.

paulocheque commented 4 years ago

Another possible solution:

teach(YourModel, your_field=lambda x: YourModel.objects.last().id+1)

Then it will simulate a SequentialDataFixture for you. Maybe I will include something like this into the core.