Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select 3 random items and exclude the one already selected

I’m trying to display three random drinks in my template, selected from a database of drinks.
I currently have the random selection working but also noticed theres a chance of a drink showing up twice.
How do I prevent this from happening? Is there a smarter way of doing this?

import random
def testview(request):

   
    drink1 = random.choice(DrinkRecipe.objects.all())
    drink2 = random.choice(DrinkRecipe.objects.all())
    drink3 = random.choice(DrinkRecipe.objects.all())


    context ={
            'drink1' : drink1,
            'drink2' : drink2,
            'drink3' : drink3,
        }

    return render(request,'drinks/test.html', context=context)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can prevent duplicate drinks from being selected by using the sample() function from the random module.
You can try this approach, considering you are using Django

import random
from django.core.exceptions import ObjectDoesNotExist

def testview(request):
    try:
        drinks = random.sample(list(DrinkRecipe.objects.all()), 3)
    except ValueError:
        # Handle the case when there are fewer than 3 drinks in the database
        drinks = list(DrinkRecipe.objects.all())

    context = {
        'drink1': drinks[0] if len(drinks) > 0 else None,
        'drink2': drinks[1] if len(drinks) > 1 else None,
        'drink3': drinks[2] if len(drinks) > 2 else None,
    }

    return render(request, 'drinks/test.html', context=context)

This code attempts to pick 3 different drinks randomly using the random.sample() function. If there are less than 3 drinks in the database, it catches an error and just uses all the available drinks. Finally, it adds the drinks to the context, making sure that no repeated drinks are shown in the template.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading