Skip to content

Instantly share code, notes, and snippets.

@jorisvddonk
Created July 30, 2014 14:18
Show Gist options
  • Save jorisvddonk/9fad25e43f30856e29eb to your computer and use it in GitHub Desktop.
Save jorisvddonk/9fad25e43f30856e29eb to your computer and use it in GitHub Desktop.
from django.contrib import admin
from polls.models import Poll
from polls.models import Choice
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 0
class PollAdmin(admin.ModelAdmin):
inlines = [ChoiceInline]
admin.site.register(Poll, PollAdmin)
from django.db import models
import datetime
from django.utils import timezone
from s3direct.fields import S3DirectField
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self): # Python 3: def __str__(self):
return self.question
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
image = S3DirectField(upload_to='images_s3direct')
def __unicode__(self): # Python 3: def __str__(self):
return self.choice_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment