I'm running into some problems when pulling a specific list of objects from my db. I'm picking up python/django after quite a bit of time off and feeling pretty rusty. As an exercise I am building an app that allows a group of friends to 'draft' season tickets, eg. take turns selecting which games you want to attend until all the tickets are distributed.
The way I've chosen to go about this is to have one db model that contains a dump of all games for an entire season (SportSeason) and a db that contains draft information (TicketDraft). As there will be many rows (to track ticket/game selection) for each draft session, I've created a UUID field for logically grouping the rows that belong to the same draft. Here is an example of my TicketDraft database that is populated with two active draft sessions (Detroit and San Jose).
id(pk) | home | away | date | session_status | uuid |
---|---|---|---|---|---|
1 | Detroit | Pittsburgh | Jan 19 | ACTIVE | d14c4 |
2 | Detroit | Las Vegas | Jan 22 | ACTIVE | d14c4 |
3 | Detroit | Seattle | Jan 23 | ACTIVE | d14c4 |
4 | San Jose | Toronto | Jan 10 | ACTIVE | 35f405 |
5 | San Jose | Edmonton | Jan 12 | ACTIVE | 35f405 |
6 | San Jose | Calgary | Jan 14 | ACTIVE | 35f405 |
7 | Vancouver | Seattle | Jan 11 | INACTIVE | 66e427 |
I'm having troubles getting unique rows based on uuid after filtering for active session_status. Below is the queryset I would like to receive, however, which occurrence selected in the queryset isn't important as long as I pull one for each active UUID.
id(pk) | home | away | date | session_status | uuid |
---|---|---|---|---|---|
1 | Detroit | Pittsburgh | Jan 19 | ACTIVE | d14c4 |
4 | San Jose | Toronto | Jan 10 | ACTIVE | 35f405 |
Passing values to distinct isn't an option as I am using mysql.
models.py
class SportSeason(models.Model):
year = models.IntegerField(default=None)
sport = models.CharField(max_length=10, default=None)
home = models.CharField(max_length=10)
visitor = models.CharField(max_length=10)
date = models.DateTimeField(auto_now=False)
class Meta:
app_label = 'ticket_draft'
class TicketDraft(models.Model):
tt_group = models.IntegerField()
tt_status = models.CharField(max_length=10) # available, selected
tt_owner = models.CharField(max_length=30)
gm_date = models.DateTimeField(auto_now=False, null=True)
team_home = models.CharField(max_length=10)
team_visitor = models.CharField(max_length=10)
draft_year = models.IntegerField()
ssn_create = models.DateTimeField(auto_now=True)
ssn_owner = models.CharField(max_length=30)
ssn_status = models.CharField(max_length=10) # active, cancelled, complete
ssn_ident = models.UUIDField(default=uuid.uuid4, editable=False)
views.py
def home_page_view(request):
if request.method == 'POST':
team = request.POST.get('home_team')
tt_groups = int(request.POST.get('ticket_groups'))
season = SportSeason.objects.filter(home=team)
session_id = uuid.uuid4()
for group in range(tt_groups):
for game in season:
row = TicketDraft(team_home=game.home,
team_visitor=game.visitor,
gm_date=game.date,
draft_year=game.year,
tt_group=group 1,
tt_status="AVAILABLE",
ssn_owner="rsw",
ssn_status="ACTIVE",
ssn_ident=session_id,
)
row.save()
return render(request, "index/index.html")
else:
sessions = TicketDraft.objects.filter(ssn_status="ACTIVE")
new_draft = CreateDraft()
context = {'arena_mappings': ARENA_MAPPINGS,
'team_mappings': TEAM_MAPPINGS,
'sessions': sessions,
'new_draft': new_draft,
}
return render(request, "index/index.html", context)
As expected, in the else: (GET) section of the above view I am returning every single game into sessions. I'm unsure if there is a clean solution here; or I have a fundamental problem with how I've decided to organize my data.
Thanks!
CodePudding user response:
I recommend you to read this section of Django official documentation which explains how distinct
works.
Assuming you're using a PostgreSQL database, you need to add .order_by("ssn_ident")
before the .distinct
method:
sessions = TicketDraft.objects.filter(ssn_status="ACTIVE").order_by("ssn_ident").distinct("ssn_ident")
CodePudding user response:
Thanks for the responses, could have been more clear in my original post with regards to using mysql. I've just solved this with python rather than trying to further filter the stream.
active = list(TicketDraft.objects.filter(ssn_status__exact="ACTIVE").values('ssn_ident',
'team_home',
'ssn_owner',
'draft_year'))
sessions = [dict(t) for t in {tuple(d.items()) for d in active}]