vendredi 22 décembre 2017

Python type error with a random number

I have the following code in my views in Django:

class singlePull(TemplateView):
template_name = 'gacha/singlepull.html'

def randomStar(self):
    choice = [5,4,3]
    probability = [0.1, 0.2, 0.7]
    star = random.choices(choice, probability)
    return star


def post(self, request):
    result = self.randomStar()
    character = Characters.objects.filter(stars=result).order_by('?')[:1]
    return JsonResponse(result, safe=False)

With this code I first randomly select a number from the available list(5,4,3) and the number selected is used as a variable in mysql query as a value. For example if number 3 is selected then the query will look for all characters in database that have stars set to 3 and order them randomly and display only one. When I run this code I keep getting the following error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

When I send result in Jsonresponse I get a random number and it's not a list. How come when I enter it in the query it sees it as a list? How can this be redone to work?

Thanks




Aucun commentaire:

Enregistrer un commentaire