Sunday, May 6, 2007

Cities & Knightsunlock

begin with Django 3 - Database

I begin with Django 3 Since I have already prepared a database, then it would be worthwhile to use it. Until that is found only in the 17th chapter DB python mysite / manage.py inspectdb
(DB:
python mysite / manage.py inspectdb> mysite / myapp / models.py ). Generated is something like this:
class Categorie (models.Model): id = models.IntegerField (primary_key = True) name =
models.CharField (maxlength = 600)
models.TextField description = (blank = True) class Meta
: db_table = 'categories'



Because the tables have different names adapted to the framework, so their names are in the plural. It seems that Django uses a simple rule, subtract from the plural 's' and you have the singular. Does not always verify that the DB write to review and clean up the generated model will do it later, according to the principles described in the said 17th section The other puzzling thing is the value of maxlength (database fields are varchar (200) and model 600), you will need to decipher how the character encoding. Unfortunately, directly in the DB's on this subject did not write.
To check whether the model works, you can use
. / Manage.py shell
. If you have previously installed the ipython, we have at the moment quite a nice work environment with a database, and in fact with its model. I dare to believe that it will also be a good place to start such unit tests. To use the model of some table, enough to enter
project.app.models import from nazwa_modelu

ie to import from mdjango.mbegin.models Categorie
, then you're ready Categorie.objects.values \u200b\u200b() (the value of the records), or Categorie.objects.count () (number of records). Another big plus for Django, you can see immediately how the model before using it in an application. For now, only I used phpmyadmin to check whether the model is correct, most of the required operations can now be done from the shell, with the assurance that you are connected to the right base, etc.

0 comments:

Post a Comment