Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. class ChoiceInline(admin.TabularInline):
  2. model = Choice
  3. extra = 3
  4.  
  5.  
  6. class BallotAdmin(admin.ModelAdmin):
  7. fieldsets = [
  8. (None, {'fields': ['ballot_name']}),
  9. (None, {'fields': ['ballot_address']}),
  10. ('Date information', {'fields': ['pub_date'], 'classes': ['expand']}),
  11. ('Date information', {'fields': ['end_date'], 'classes': ['expand']}),
  12. ]
  13. inlines = [ChoiceInline]
  14.  
  15. # include a list filter
  16. list_filter = ['pub_date']
  17. list_display = ('ballot_name', 'ballot_address', 'pub_date', 'end_date', 'was_published_recently')
  18.  
  19.  
  20. # include a ballot search
  21. search_fields = ['ballot_name']
  22.  
  23. def save_model(self, request, obj, form, change):
  24. print("***************************************SANDEEPPP*****************************************")
  25.  
  26. print('Ballot address is: ', obj.ballot_address)
  27.  
  28. super().save_model(request, obj, form, change)
  29.  
  30. admin.site.register(Ballot, BallotAdmin)
  31.  
  32. class Ballot(models.Model):
  33. ballot_name = models.CharField(max_length=200)
  34. ballot_address = models.CharField(max_length=36)
  35. pub_date = models.DateTimeField('date published')
  36. end_date = models.DateTimeField('end date')
  37. def __str__(self):
  38. return self.ballot_name
  39.  
  40. def was_published_recently(self):
  41. now = timezone.now()
  42. return now - datetime.timedelta(days=1) <= self.pub_date <= now
  43. was_published_recently.admin_order_field = 'pub_date'
  44. was_published_recently.boolean = True
  45. was_published_recently.short_description = 'Published recently?'
  46.  
  47. class Choice(models.Model):
  48. ballot = models.ForeignKey(Ballot, on_delete=models.CASCADE)
  49. voter_text = models.CharField(max_length=200)
  50. voter_address = models.CharField(max_length=36)
  51. #votes = models.IntegerField(default=0)
  52.  
  53. def __str__(self):
  54. return self.voter_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement