Automatically Creating Initial_data fixtures for Sites.Site

This is an ultra-hacky way to create initial_data fixtures for django admin's Sites.site.  Create a variable in settings.py such as "FQDN" then create a dictionary with all the values and wrap it with a list, import json and dump the list into json and write it to app_name/fixtures/initial_data.json


settings.py:

#ADD your own variable
FQDN="www.whateversite.com"
full_path = 'path/to/project/project_name/app_name/fixtures/initial_data.json'
print full_path

try:
   with open(full_path) as f: pass
except IOError as e:
   print str(e)
   site_fixture_dict = {}
   site_fixture_dict['pk']=1
   site_fixture_dict['model']="sites.site"
   site_fixture_dict['fields']={'name':FQDN,'domain':FQDN}
   site_fixture_list = [site_fixture_dict]
   import json
   json = json.dumps(site_fixture_list)
   f = open(full_path,'w')
   f.write(json)
   f.close()

Now any time you run ./manage.py syncdb a fixture will be created then loaded into the database

Tagged as django , fixtures settings.py , site sites.site
Written by Andrew Konkol on August 1st, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.