F () Expressing strange behavior with Django

I have this template tag that ultimately returns a list of active ads (checks if Campaign with an active field True

and then pulls ads from the Campaign with a request)

@register.assignment_tag
def get_current_campaigns(amount):

    # Get all the campaigns that are active 
    current_campaigns = Campaign.objects.filter(active=True)

    current_campaigns_count = current_campaigns.count()

    # To avoid the list index being out of range and throwing an IndexError
    # We reduce the amount to match the amount of rows in the model if the
    # amount of rows is less than the amount being requested.
    if amount > current_campaigns_count:
        amount = current_campaigns_count

    # Select active campaigns randomly
    random_camps = []
    for i in range(amount):
        random_camps.append(random.choice(current_campaigns))

    # prepare all the ads to return 
    output = []
    for campaign in random_camps:
        # get all the ads that a campaign has 
        ads = campaign.advertisement_set.all()
        # now select one randomly
        ad = random.choice(ads)
        # hand it to output 
        output.append(ad)
        # mark that this campaign has been seen
        campaign.impressions = F('impressions') + 1
        campaign.save()
        # checks and sets if the campaign is still active
        campaign.check_active()

    return output

      

And here's the model that comes with it:

class Campaign(models.Model):
    ''' Represents an Advertisement Campaign '''
    title = models.CharField(max_length=50, blank=True, verbose_name='Campaign Title')
    impressions = models.IntegerField()
    impression_limit = models.IntegerField()
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)

    def check_active(self):
        ''' Checks if the Campaign is currently active '''
        if self.impressions >= self.impression_limit:
            self.active = False
            self.save()

      

Weird bit: every time I visit a page that has an ad enabled, and then check it in the admin, object impressions are incremented by 2 (it should be 1) and are marked as False, even if this one is if self.impressions >= self.impression_limit

not true, it doesn't matter how This changes the active field to False

independently.

Is there any clue why this strange behavior is happening? If necessary, I can provide additional information.

+3


source to share


1 answer


random.choice

does not guarantee that you will receive non-repeating items.

import random

random_camps = random.sample(current_campaigns, amount)

      



- the way here.

Update If you are worried about speed this question , we recommend picking random lines quickly in postgres.

+4


source







All Articles