Code

Changes between Initial Version and Version 1 of InitialSQLDataDiangoORMWay


Ignore:
Timestamp:
09/09/06 03:37:47 (8 years ago)
Author:
riklaunim@…
Comment:

Page Creation

Legend:

Unmodified
Added
Removed
Modified
  • InitialSQLDataDiangoORMWay

    v1 v1  
     1Django 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. 
     2 
     3 
     4= Initial SQL data using Django ORM = 
     5 * Create in the main django project folder file called '''install.py''' 
     6 * Add the code: 
     7{{{ 
     8from os import environ 
     9environ['DJANGO_SETTINGS_MODULE'] = 'settings' 
     10 
     11from settings import * 
     12}}} 
     13And 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. 
     14 
     15 
     16=== How should it be used? === 
     171. run '''python manage.py syncdb''' 
     18 
     192. run '''python install.py''' 
     20 
     21install.py should be run after creating tables with syncdb. 
     22 
     23 
     24== Adding Data == 
     25Import your models and add new objects like in views, for example: 
     26{{{ 
     27from myghtyboard.models import * 
     28mc = Category(cat_name='First Category', cat_order='0') 
     29mc.save() 
     30mc = Category(cat_name='Second Category', cat_order='1') 
     31mc.save() 
     32 
     33// we should use Category.objects.get(cat_name=....) but im lazy... 
     34mf = 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>') 
     35mf.save() 
     36mf = Forum(forum_category = Category.objects.get(id=1), forum_name = 'Second Forum', forum_description ='A description', forum_order='1') 
     37mf.save() 
     38mf = Forum(forum_category = Category.objects.get(id=2), forum_name = 'Bla bla bla', forum_description ='Extra Forum', forum_order='0') 
     39mf.save() 
     40}}} 
     41We 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 :) 
     42 
     43 
     44== How to add a group and assign permissions to it ? == 
     45{{{ 
     46from django.contrib.auth.models import Group, Permission 
     47g = Group(name='users') 
     48g.save() 
     49g.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')) 
     50}}} 
     51Import 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: 
     52{{{ 
     53a = Permission.objects.all() 
     54for i in a: 
     55        print str(i.name) + ' - ' + str(i.codename) 
     56}}}