32

I want to test models which are explained in the Django tutorial. Is there an automatic way to fill them with sample data? It's one of them:

class Book(models.Model):
    name = models.CharField(max_length=300)
    pages = models.IntegerField()
    price = models.DecimalField(max_digits=10, decimal_places=2)
    rating = models.FloatField()
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
    pubdate = models.DateField()

Any suggestion?

Ehsan
  • 3,711
  • 27
  • 30
Joe
  • 3,059
  • 2
  • 22
  • 28
  • possible duplicate of [Making a django Fixture that gets 100 items from each Table, or follows foreign keys](http://stackoverflow.com/questions/5599381/making-a-django-fixture-that-gets-100-items-from-each-table-or-follows-foreign-k) – dting Apr 13 '11 at 04:03
  • Possible duplicate of [How to 'bulk update' with Django?](http://stackoverflow.com/questions/12661253/how-to-bulk-update-with-django) | http://stackoverflow.com/questions/16853649/executing-python-script-from-django-shell – Ciro Santilli OurBigBook.com May 13 '16 at 14:11

6 Answers6

25

I haven't used it myself, but django-autofixture looks pretty much like what you are after.

Other similar apps are listed in this grid: https://www.djangopackages.com/grids/g/fixtures/

Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80
arie
  • 18,737
  • 5
  • 70
  • 76
  • That looks neat, similar to dilla by the look of it. – Josh Smeaton Apr 13 '11 at 11:28
  • 2
    django-mockups seems to be an even more recent fork of django-autofixtures: https://github.com/sorl/django-mockups. guess i should take a closer look at these tools too :-) – arie Apr 13 '11 at 12:25
  • 1
    It looks like activity on `sorl/django-mockups` stopped a few years ago, while django-autofixture has had more recent activity. – CoatedMoose Jan 28 '15 at 21:44
  • 1
    Not working with django3.0 :( Required django.utils.six – GrvTyagi Dec 28 '19 at 15:15
8

http://www.generatedata.com/

This has some pretty nice generic field types that aren't Django-specific

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
8

django-dilla was built specifically to populate your django models with 'spam' data. The below is taken directly from the site example after defining some settings. It will even let you define your own 'spammers' that will generate data in a particular format.

$ ./manage.py run_dilla --cycles=100
Dilla is going to spam your database. Do you wish to proceed? (Y/N)Y
Dilla finished!
    2 app(s) spammed 900 row(s) affected, 2498 field(s) filled, \
    502 field(s) ommited.
CoatedMoose
  • 3,624
  • 1
  • 20
  • 36
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
2

Checkout django-mockups: https://github.com/sorl/django-mockups

It will automatically generate data for any model, including foreign key and many to many. You can run it as-is out-of-the-box, giving it the maximum depth for relationships, and it will generate data fully exploiting your model.

You can also write your own generators and factories to get fine-grained control over relationships and to generate data specific to your application, rather than just random data. I just got through using it on a project, and it saved me literally days of work setting up test data.

MaDeuce
  • 829
  • 6
  • 2
  • 1
    It looks like activity on this fork stopped a few years ago, while django-autofixture has had more recent activity. – CoatedMoose Jan 28 '15 at 21:37
1

If the question is still actual you may try package django-mimesis. It offers to fill database with dummy data in different languages according to types of fields. Also you may download some pictures automatically using specified topics. Not always works great, sometime pictures' topics are mismatched. But for me it is enough.

Naaim Iss
  • 181
  • 1
  • 9
0

Django-eadred was designed for "generating sample data."

As its docs state

eadred allows you to programmatically generate the data using model makers, factories, fixtures, random seeds—whatever your needs are.

Additionally, eadred provides library functions to make generating data easier.

Community
  • 1
  • 1
Private
  • 2,626
  • 1
  • 22
  • 39