Changes between Initial Version and Version 1 of InitialSQLDataDiangoORMWay

09/09/06 10:37:47 (16 years ago)

Page Creation


  • 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.
     4= Initial SQL data using Django ORM =
     5 * Create in the main django project folder file called ''''''
     6 * Add the code:
     8from os import environ
     9environ['DJANGO_SETTINGS_MODULE'] = 'settings'
     11from settings import *
     13And this is the 100% of magic. We set DJANGO_SETTINGS_MODULE for Django (which is refering to in the same folder) and then import in the python sense.
     16=== How should it be used? ===
     171. run '''python syncdb'''
     192. run '''python'''
     20 should be run after creating tables with syncdb.
     24== Adding Data ==
     25Import your models and add new objects like in views, for example:
     27from myghtyboard.models import *
     28mc = Category(cat_name='First Category', cat_order='0')
     30mc = Category(cat_name='Second Category', cat_order='1')
     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>')
     36mf = Forum(forum_category = Category.objects.get(id=1), forum_name = 'Second Forum', forum_description ='A description', forum_order='1')
     38mf = Forum(forum_category = Category.objects.get(id=2), forum_name = 'Bla bla bla', forum_description ='Extra Forum', forum_order='0')
     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 :)
     44== How to add a group and assign permissions to it ? ==
     46from django.contrib.auth.models import Group, Permission
     47g = Group(name='users')
     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'))
     51Import models and add. You can find something about adding permissions to user/group in the auth docs, but more help '''django/contrib/auth/''' 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:
     53a = Permission.objects.all()
     54for i in a:
     55        print str( + ' - ' + str(i.codename)
Back to Top