File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1
1
from django .contrib import admin
2
2
3
3
# 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 )
Original file line number Diff line number Diff line change 1
1
from django .db import models
2
+ from django .utils import timezone
3
+ import datetime
2
4
3
5
4
6
class Question (models .Model ):
5
7
question_text = models .CharField (max_length = 200 )
6
8
pub_date = models .DateTimeField ('date published' )
7
9
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
+
8
22
9
23
class Choice (models .Model ):
10
24
question = models .ForeignKey (Question )
11
25
choice_text = models .CharField (max_length = 200 )
12
26
votes = models .IntegerField (default = 0 )
27
+
28
+ def __str__ (self ):
29
+ return self .choice_text
You can’t perform that action at this time.
0 commit comments