Django自定义管理表单
修改polls/admin.py:
from django.contrib import admin<br /><br />from .models import Choice, Question<br /><br /><br />class ChoiceInline(admin.TabularInline):<br /> model = Choice<br /> extra = 3<br /><br /><br />class QuestionAdmin(admin.ModelAdmin):<br /> fieldsets = [<br /> (None, {'fields': ['question_text']}),<br /> ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),<br /> ]<br /> inlines = [ChoiceInline]<br /> list_display = ('question_text', 'pub_date', 'was_published_recently')<br /> list_filter = ['pub_date']<br /> search_fields = ['question_text']<br /><br /><br />admin.site.register(Question, QuestionAdmin)<br />
修改polls/models.py:

import datetime<br /><br />from django.db import models<br />from django.utils import timezone<br /><br /><br />class Question(models.Model):<br /> question_text = models.CharField(max_length=200)<br /> pub_date = models.DateTimeField('date published')<br /><br />def __str__(self):<br />return self.question_text<br /><br />def was_published_recently(self):<br /> now = timezone.now()<br />return now - datetime.timedelta(days=1) <= self.pub_date <= now<br /><br /> was_published_recently.admin_order_field = 'pub_date'<br /> was_published_recently.boolean = True<br /> was_published_recently.short_description = 'Published recently?'<br /><br /><br />class Choice(models.Model):<br /> question = models.ForeignKey(Question, on_delete=models.CASCADE)<br /> choice_text = models.CharField(max_length=200)<br /> votes = models.IntegerField(default=0)<br /><br />def __str__(self):<br />return self.choice_text<br />
启动服务:
python manage.py runserver
访问:
http://127.0.0.1:8000/admin/polls/question/




