DJango Memo: From the beginning (model settings)

This is a continuation of DJango Note: From the Beginning.

Creating and applying a model

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.

Creating models (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.

Apply model (settings.py)
#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.

Studying database API

Reference: http://www.djangoproject.jp/doc/ja/1.0/topics/db/queries.html#topics-db-queries

Recommended Posts

DJango Memo: From the beginning (model settings)
DJango Memo: From the beginning (Error screen settings)
DJango Memo: From the beginning (preparation)
DJango Memo: From the beginning (creating a view)
DJango Memo: From the beginning (more edits to the management screen)
DJango Note: From the beginning (form processing)
DJango memo: From the beginning (using the management screen) my addictive point
Django memo # 1 from scratch
DJango memo: From the beginning (editing the management screen) There is a mystery
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
Use django model from interpreter
DJango Note: From the beginning (simplification and splitting of URLConf)
The story of a Django model field disappearing from a class
Get the value from the [Django] Form
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
Learning notes from the beginning of Python 2
Get only the text from the Django form.
Django model: ManyToManyField
Django Learning Memo
django tutorial memo
django default settings
Finding the beginning of Abenomics from NT magnification 2
Django + MySQL settings
Finding the beginning of Abenomics from NT magnification 1
Django URL settings
The wall of changing the Django service from Python 2.7 to Python 3
Learn Nim with Python (from the beginning of the year).
Study from the beginning of Python Hour1: Hello World
Mathematical understanding of principal component analysis from the beginning
Make the model a string on a Django HTML template
Study from the beginning of Python Hour8: Using packages
[Django] Give Form a dynamic initial value from Model
The beginning of cif2cell
Analyze the illustration (Memo)
heroku deployment memo (Django)
Model changes in Django
High Performance Django --Model
[Django] Rename the project
Django command completion settings
[Memo] Django development environment
[Django] Manage settings like writing in settings.py with a model
PyTorch learning memo (I made the same model as Karas)
How to do the initial setup from Django project creation