Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChoiceInline(admin.TabularInline):
- model = Choice
- extra = 3
- class BallotAdmin(admin.ModelAdmin):
- fieldsets = [
- (None, {'fields': ['ballot_name']}),
- (None, {'fields': ['ballot_address']}),
- ('Date information', {'fields': ['pub_date'], 'classes': ['expand']}),
- ('Date information', {'fields': ['end_date'], 'classes': ['expand']}),
- ]
- inlines = [ChoiceInline]
- # include a list filter
- list_filter = ['pub_date']
- list_display = ('ballot_name', 'ballot_address', 'pub_date', 'end_date', 'was_published_recently')
- # include a ballot search
- search_fields = ['ballot_name']
- def save_model(self, request, obj, form, change):
- print("***************************************SANDEEPPP*****************************************")
- print('Ballot address is: ', obj.ballot_address)
- super().save_model(request, obj, form, change)
- admin.site.register(Ballot, BallotAdmin)
- class Ballot(models.Model):
- ballot_name = models.CharField(max_length=200)
- ballot_address = models.CharField(max_length=36)
- pub_date = models.DateTimeField('date published')
- end_date = models.DateTimeField('end date')
- def __str__(self):
- return self.ballot_name
- 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):
- ballot = models.ForeignKey(Ballot, on_delete=models.CASCADE)
- voter_text = models.CharField(max_length=200)
- voter_address = models.CharField(max_length=36)
- #votes = models.IntegerField(default=0)
- def __str__(self):
- return self.voter_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement