Accessing single value from multiple values from databse

9 views
Skip to first unread message

Mayur Bagul

unread,
Apr 30, 2019, 6:57:33 AM4/30/19
to Django users
Hello community,

i have created three fields in my model Data 
  • Two-wheeler
  • four-wheeler
inside each field i have putted 5 values.

now i wanted to display single random value from these two fields every time i click on button. and thats where im getting stuck.
How to do this ?

if any one have resources related to this issue please provide me and help me with your solution.



Thanking you.

Derek

unread,
May 2, 2019, 4:11:46 AM5/2/19
to Django users
The logic would be something along these lines (note that you'd need to sort out the "button click" side)

# class Car(models.Model):
#     two_wheeler = models.CharField(validators=[validate_comma_separated_integer_list])

import random
car = Car.objects.get(pk=1)
numbers = car.two_wheeler.split(',')
random_item = random.sample(numbers, 1)
# repeat for the other similar field(s)
Reply all
Reply to author
Forward
0 new messages