查看安装版本:
import django
print(django.get_version\(\)) # 1.8.2
1 |
|
django-admin startproject mysite
1 |
|
mysite/
manage.py
mysite/
init.py
settings.py
urls.py
wsgi.py
1 |
|
python manage.py migrate
1 |
|
python manage.py runserver 8080
1 |
|
python manage.py startapp polls
1 |
|
polls/
init.py
admin.py
migrations/
init.py
models.py
tests.py
views.py
1 |
|
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
1 |
|
python manage.py makemigrations polls
1 |
|
BEGIN;
CREATE TABLE “polls_choice” (
“id” serial NOT NULL PRIMARY KEY,
“choice_text” varchar(200) NOT NULL,
“votes” integer NOT NULL
);
CREATE TABLE “polls_question” (
“id” serial NOT NULL PRIMARY KEY,
“question_text” varchar(200) NOT NULL,
“pub_date” timestamp with time zone NOT NULL
);
ALTER TABLE “polls_choice” ADD COLUMN “question_id” integer NOT NULL;
ALTER TABLE “polls_choice” ALTER COLUMN “question_id” DROP DEFAULT;
CREATE INDEX “polls_choice_7aa0f6ee” ON “polls_choice” (“question_id”);
ALTER TABLE “polls_choice”
ADD CONSTRAINT “polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id”
FOREIGN KEY (“question_id”)
REFERENCES “polls_question” (“id”)
DEFERRABLE INITIALLY DEFERRED;
COMMIT;
1 |
|
from polls.models import Question, Choice
Question.objects.all()
[]
from django.utils import timezone
q = Question(question_text=”What’s new?”, pub_date=timezone.now())
q.save()
q.id
1
q.question_text
“What’s new?”
q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=)
# Change values by changing the attributes, then calling save\(\).
>>> q.question_text = "What's up?"
>>> q.save\(\)
# objects.all\(\) displays all the questions in the database.
>>> Question.objects.all\(\)
[<Question: Question object>]
1 |
|
python manage.py createsuperuser
Username:admin
Email address:admin@example.com
password:
Password(again):
Superuser created successfully.
1 |
|
from django.contrib import admin
from .models import Question
admin.site.regitster(Question)
1 |
|
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.MOdelAdmin):
fields = ['pub_date', 'question_text']
admin.site.register(Question,QuestionAdmin)
1 |
|
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {‘fields’: [‘question_text’]}),
(‘Date information’, {‘fields’: [‘pub_date’]}),
]
1 |
|
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {‘fields’: [‘question_text’]}),
(‘Date information’, {‘fields’: [‘pub_date’], ‘classes’: [‘collapse’]}),
]
1 |
|
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Question, QuestionAdmin)
1 |
|
class ChoiceInline(admin.TabularInline):
#...
1 |
|
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display = ('question_text', 'pub_date', 'was_published_recently')
1 |
|
class Question(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now\(\) - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
可以使用list_filter
选项来增加右侧的过滤器。例如使用list_filter =['pub_date']
让系统自动的为我们生成”今天“,”最近7天“,”本月“,”本年“,”不限“等选项。
可以为列表的表头中添加搜索列,使用 search_fields
选项,例如使用search_fields = ['question_text']
选项来让列表中可以根据标题来搜索选项。
调整每一页显示的数量