Version 1 (modified by riklaunim@…, 18 years ago) ( diff )

Page Creation

Django allows you to put initial SQL data using plain files with RAW SQL data, which would force you to make such file for each database and if you depend on some django data (groups, users) then IDs in the SQL dump may be wrong if someone has bit different django project running.

Initial SQL data using Django ORM

  • Create in the main django project folder file called install.py
  • Add the code:
    from os import environ
    environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    
    from settings import *
    

And this is the 100% of magic. We set DJANGO_SETTINGS_MODULE for Django (which is refering to settings.py in the same folder) and then import settings.py in the python sense.

How should it be used?

  1. run python manage.py syncdb
  1. run python install.py

install.py should be run after creating tables with syncdb.

Adding Data

Import your models and add new objects like in views, for example:

from myghtyboard.models import *
mc = Category(cat_name='First Category', cat_order='0')
mc.save()
mc = Category(cat_name='Second Category', cat_order='1')
mc.save()

// we should use Category.objects.get(cat_name=....) but im lazy...
mf = Forum(forum_category = Category.objects.get(id=1), forum_name = 'First Forum', forum_description ='A Forum', forum_order='0', forum_posts='4', forum_lastpost = 'piotr<br>2006-09-04 15:56:29<br><a href="/forum/topic/1/2/">Frugalware Topic</a>')
mf.save()
mf = Forum(forum_category = Category.objects.get(id=1), forum_name = 'Second Forum', forum_description ='A description', forum_order='1')
mf.save()
mf = Forum(forum_category = Category.objects.get(id=2), forum_name = 'Bla bla bla', forum_description ='Extra Forum', forum_order='0')
mf.save()

We imported a model and added some data. In the Forum case (which are many2one with Category) we need to pass the Category object :) And don't use IDs in get() when refering to "external" or other data that may be different than in your install :)

How to add a group and assign permissions to it ?

from django.contrib.auth.models import Group, Permission
g = Group(name='users')
g.save()
g.permissions.add(Permission.objects.get(codename='can_view'), Permission.objects.get(codename='can_set_current'), Permission.objects.get(codename='add_page'), Permission.objects.get(codename='change_page'), Permission.objects.get(codename='add_topic'), Permission.objects.get(codename='add_post'))

Import models and add. You can find something about adding permissions to user/group in the auth docs, but more help django/contrib/auth/models.py will probably provide. In the example I've added permission from Page model (add_MODEL, change_MODEL etc. are standard codenames for each model). If you have problems with getting right codenames:

a = Permission.objects.all()
for i in a:
	print str(i.name) + ' - ' + str(i.codename)
Note: See TracWiki for help on using the wiki.
Back to Top