Django model : DB
1. Each model : a single database table
2. each attribute of the model : a column in the database table
3. attribute name : the column’s name
4. type of field (e.g., CharField) : database column type (e.g., varchar)
5. instance of model : row in the database table
If you want to use Django’s database layer, i.e., model,
you need to create a Django app.
1. constants or list of choices
2. full list of fields
3. Meta class
4. __unicode__() method
5. save()
6. get_absolute_url()
7. any additional custom methods
After you created your models, you need to tell django to use it.
To let django know that, you need to add your model name at INSTALLED_APPS in settings.py.
e.g.,
———
INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
‘django.contrib.admin’,
‘myblogsite.myblogapp’,
)
Model
maps to a single database table.
Model
is about your data.
Model contains
1. fields
2. methods
Model is similar to the concept of class in other object-oriented programming.
After you generate a model, if you register the model at admin site,
admin can create data based on your model design (even if you haven’t built the input page yet.)
————
e.g.,
from django.contrib import admin
from yourapp.models import Post
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {’slug’: (’title’,)}
admin.site.register(Post, PostAdmin)
