简体   繁体   中英

Django models retrieve specific records daily

I have a django application that will retrieve and display 3 records (questions) to all users of the app during a 24hr window beginning at a specific time, say 11:00 daily. Preferably, I would like to randomise the list of records or just use the question id which autoincrements. There are more than 14,000 records.

I can retrieve records from the db and randomly display those records. However, every http get request (refreshing browser window) retrieves different questions Models.py


    from django.db import models
    from django.contrib.auth.models import User
    from django.conf import settings

    class Questions(models.Model):
        question = models.CharField(max_length=254, blank=True, null=True)
        answer = models.CharField(max_length=254, blank=True, null=True)
        question_used = models.BooleanField(default=False)
        created_at = models.DateTimeField(auto_now_add=True)
        modified_at = models.DateTimeField(auto_now_add=True)

Views.py

    from django.shortcuts import render
    from django.views.generic import CreateView, View
    from .models import *
    from wallet.models import UserWallet

    class DailyDrawView(View):
        template_name = 'daily_draw/daily_draw.html'

        def get(self, request, *args, **kwargs):
            que_attempt = DailyDraw.objects.filter(user=request.user, is_active=True).last()
            question_objs = Questions.objects.all().order_by('?')[:3]
            points = WinnerPoint.objects.last().amount
            return render(request, self.template_name {'question_objs':question_objs, 'que_attempt':que_attempt, 'points':points })

At a particular time daily, retrieve 3 records. Display those records to all users throughout a 24hrs period. Update those 3 rows so that those same records are not displayed again. Rinse and repeat.

So my solution was to create a separate table that stored question ids and date. Then compared the time when a request is made to a constant time when new questions should be generated. If the time was past 24hrs, I updated the reference table with a new row of question ids and the current date.

I can post a more comprehensive answer if anyone requires it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM