Skip to content

Commit dc5d8ff

Browse files
committed
admin: adding questions and choices and customisations as for tutorial part 02
1 parent 24def5c commit dc5d8ff

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

mysite/polls/admin.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
from django.contrib import admin
22

33
# Register your models here.
4+
from .models import Choice, Question
5+
6+
7+
class ChoiceInline(admin.TabularInline):
8+
model = Choice
9+
extra = 3
10+
11+
12+
class QuestionAdmin(admin.ModelAdmin):
13+
fieldsets = [
14+
(None, {'fields': ['question_text']}),
15+
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
16+
]
17+
inlines = [ChoiceInline]
18+
list_display = ('question_text', 'pub_date', 'was_published_recently')
19+
list_filter = ['pub_date']
20+
search_fields = ['question_text']
21+
22+
admin.site.register(Question, QuestionAdmin)

mysite/polls/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
from django.db import models
2+
from django.utils import timezone
3+
import datetime
24

35

46
class Question(models.Model):
57
question_text = models.CharField(max_length=200)
68
pub_date = models.DateTimeField('date published')
79

10+
def __str__(self):
11+
return self.question_text
12+
13+
def was_published_recently(self):
14+
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
15+
16+
# Admin view attributes.
17+
was_published_recently.admin_order_field = 'pub_date'
18+
was_published_recently.boolean = True
19+
was_published_recently.short_description = 'Published recently?'
20+
21+
822

923
class Choice(models.Model):
1024
question = models.ForeignKey(Question)
1125
choice_text = models.CharField(max_length=200)
1226
votes = models.IntegerField(default=0)
27+
28+
def __str__(self):
29+
return self.choice_text

0 commit comments

Comments
 (0)