Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto populate when syncdb with fixture for django-site

i want to populate django_site table when i run after syncdb initially how can i do that i have one site only

like image 693
soField Avatar asked Oct 14 '10 14:10

soField


2 Answers

You can either use the admin interface, from the shell, or script it (if you're looking for an automated solution). Here's how to do it from the shell (and what you would put into the script):

[sledge@localhost projects]$ python manage.py shell
>>> from django.contrib.sites.models import Site
>>> newsite = Site(name="Test",domain="test.com")
>>> newsite.save()
like image 60
Andrew Sledge Avatar answered Sep 25 '22 10:09

Andrew Sledge


Simple solution is to create a initial_data.json fixture for the Sites app that will override the default.

For example, my fixture at /myproject/myapp/fixtures/initial_data.json:

[
  {
    "model": "sites.site", 
    "pk": 1, 
    "fields": {
      "domain": "myproject.mydomain.com", 
      "name": "My Project"
    }
  }
]

A little note: Because this is common data for the whole project, it might be a good idea to store the fixture to /myproject/fixtures/ or to an app /myproject/commons/ (like I do) instead storing it with just some app. This keeps the data to be easy to find and makes apps a little more reusable.

A second note: Django allows to use multiple initial_data.json fixtures in multiple apps (Using mixed set of initial_data.json and initial_data.yaml fixtures didn't work as expected though :P). They all will be automatically used to pre-populate the database when syncdb is run.

Some references:

  • Django - Providing initial data with fixtures
  • The second comment at a codespatter.com blog post
like image 37
Akseli Palén Avatar answered Sep 26 '22 10:09

Akseli Palén