Skip to content

Instantly share code, notes, and snippets.

@bencleary
Last active November 2, 2018 11:30
Show Gist options
  • Select an option

  • Save bencleary/3f674b59f1902754ce2acc7873b466df to your computer and use it in GitHub Desktop.

Select an option

Save bencleary/3f674b59f1902754ce2acc7873b466df to your computer and use it in GitHub Desktop.
from django.contrib import admin
from .models import *
class AuctionAdmin(admin.ModelAdmin):
list_display = ['title', 'current_bid_display', 'bid_count', 'expiry_date', 'active']
def current_bid_display(self, obj):
return "£{0}".format(obj.current_bid)
class BidAdmin(admin.ModelAdmin):
list_display = ['auction', 'value', 'owner', 'created_at']
list_filter = (
('auction',)
)
admin.site.register(Auction, AuctionAdmin)
admin.site.register(Bid, BidAdmin)
from django.db import models
from django.contrib.auth.models import User
class AuctionManager(models.Manager):
def active(self):
return super().get_queryset().filter(active=True)
def expired(self):
return super().get_queryset().filter(active=False)
class Auction(models.Model):
title = models.CharField(max_length=255)
current_bid = models.FloatField()
bid_count = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
live_date = models.DateTimeField()
expiry_date = models.DateTimeField()
active = models.BooleanField()
owner = models.ForeignKey(User, on_delete=None)
def __str__(self):
return self.title
def __unicode__(self):
return self.title
objects = models.Manager()
AuctionManager = AuctionManager()
class BidManager(models.Manager):
def current(self, auction):
return super().get_queryset().filter(auction=auction).order_by('-created_at')
class Bid(models.Model):
auction = models.ForeignKey(Auction, on_delete=None)
value = models.FloatField()
owner = models.ForeignKey(User, on_delete=None)
created_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
BidManager = BidManager()
from django.dispatch import receiver
from django.db.models.signals import post_save
from .models import *
@receiver(post_save, sender=Bid)
def update_auction_totals_for_bid(sender, instance, created, **kwargs):
if created:
auction = instance.auction
auction.bid_count = Bid.BidManager.current(auction).count()
auction.current_bid = instance.value
auction.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment