I am following the docs on making queries and trying to select a random URL from my database. However when I run the below code in my views.py I get:
Random URL = URLs object
This should be one of the random URLs e.g. /surveyseven/
in my MySQL DB. Can anyone tell me what I am doing wrong?
views.py
def begin(request):
surveyurls = URLs.objects.all()
random_survey_index = random.choice(surveyurls)
print 'Random URL = ', random_survey_index
models.py
class URLs(models.Model):
SURVEYONE = models.CharField(max_length=25)
SURVEYTWO = models.CharField(max_length=25)
SURVEYTHREE = models.CharField(max_length=25)
SURVEYFOUR = models.CharField(max_length=25)
SURVEYFIVE = models.CharField(max_length=25)
SURVEYSIX = models.CharField(max_length=25)
SURVEYSEVEN = models.CharField(max_length=25)
SURVEYEIGHT = models.CharField(max_length=25)
SURVEYNINE = models.CharField(max_length=25)
fixtures/URLs.json
I load the initial data using fixtures python manage.py loaddata URLs
and I can see it in my DB.
[
{
"model": "survey.URLs",
"pk": 1,
"fields": {
"SURVEYONE": "/surveyone/",
"SURVEYTWO": "/surveytwo/",
"SURVEYTHREE": "/surveythree/",
"SURVEYFOUR": "/surveyfour/",
"SURVEYFIVE": "/surveyfive/",
"SURVEYSIX": "/surveysix/",
"SURVEYSEVEN": "/surveyseven/",
"SURVEYEIGHT": "/surveyeight/",
"SURVEYNINE": "/surveynine/"
}
}
]