Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Possible to load fixtures with date fields based on the current date?

Tags:

django

I want to load fixtures into django. The data has some date fields - is it possible to create these data so they will always be e.g. yesterday or tomorrow? I want to make sure certain data is always fresh, but also so I can easily test edge cases (e.g. whether an object is enabled if the publishing date is today, etc).

like image 512
Yoki32 Avatar asked Oct 09 '10 18:10

Yoki32


1 Answers

Fixtures just load text data files (in JSON/XML/YAML) so, there's no real way to insert dynamically generated data by just loading a fixture. On the other hand, you can get around this using other methods.

One option is the package django-fixture-generator where you can write python/django code to create data and it will be inserted before your tests are called.

Another option is a previous SO question: How to load sql fixture in Django for User model?. This has some code on using SQL files for fixtures, where you can use a SQL expression for your date requirements (e.g. GETDATE()+1 or similar in your SQL dialect).

like image 192
ars Avatar answered Oct 21 '22 11:10

ars