This is a continuation of DJango Note: From the Beginning.
Create an application with the following command. The project consists of multiple applications (probably).
python manage.py startapp [polls]
[polls](directory containing \ __ init__.py models.py views.py) will be created, so edit this models.py.
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
It seems that these classes become models and database tables are constructed based on this model. However, at this point we are still defining the model, so this time edit settings.py to apply it.
#Fix this part
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites', #here
'polls' #here. It seems to be following from the root directory of the project
)
Here again
python manage.py syncdb
Execute. Then the table will be added to the previously created database.
Reference: http://www.djangoproject.jp/doc/ja/1.0/topics/db/queries.html#topics-db-queries
Recommended Posts