-1

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/"
    }
  }

]
Deepend
  • 4,057
  • 17
  • 60
  • 101
  • Deepend, you've been around SO for quite a long time, and you've been asking questions about Django for over two years. I don't understand why you are suddenly asking really basic questions that show not the slightest understanding of the simplest things in Django. – Daniel Roseman Jul 16 '15 at 20:29
  • To paraphrase Donald Rumsfeld, I don't know what I don't know. Essentially I have never coded before (except for a painful experience with Java), I am learning in isolation and I can only dedicate a small amount of time each day to trying to figure things out. So I end up asking what may be very basic questions. I'm not just learning Django, but Python as well. The questions I have been asking lately may seem basic simply because there are very large gaps in my knowledge. Despite assurances I would be attending beginner/intermediate Python courses, they never materialized. – Deepend Jul 16 '15 at 21:23
  • So while I may not give much back to the Django community here as I can rarely answer the questions asked. I try to do as much moderation as my rep will allow each day to pay back what I get out of this site. I am also keen to learn and try to use this site and Code Review to learn new things and see what I can improve and where I have went wrong. I apologize if you feel I have wasted anybody's time here, but I have gained so much here and I am genuinely appreciative of everyone's help. – Deepend Jul 16 '15 at 21:28
  • I appreciate that, and I'm not trying to discourage you from asking questions, but it seems strange that after two years of apparently working with Django, you're suddenly back to a situation where you're confused between fields and models. – Daniel Roseman Jul 16 '15 at 21:43
  • Perhaps you are correct though, on two fronts. I tend to focus to much on the hurdle on front of me and not on the race. I will take a step back tomorrow and reread some of the docs, especially the overview ones. I should be able to glean a much better understanding of the framework now that I have been actively using it, well trying to anyway . I also might have become over reliant on the community here to solve my problems without putting sufficient effort in myself. I just hate the time wasted looking in every rabbit hole. But I will endeavour to put some more study in. – Deepend Jul 16 '15 at 22:07

1 Answers1

0
class URL(models.Model):
    location = models.CharField(max_length=25)


[
    {
        "model": "survey.URL",
        "pk": 1,
        "fields": {
             "location": "/surveyone/"
        }
    },
    {
        "model": "survey.URL",
        "pk": 2,
        "fields": {
             "location": "/surveytwo/"
        }
    },
    {
        "model": "survey.URL",
        "pk": 3,
        "fields": {
             "location": "/surveythree/"
        }
    },

     ...
]


def begin(request):
    random_survey_obj = URL.objects.order_by('?').first()
    print('Random URL = {0}'.format(random_survey_obj.location))

about order_by('?') read here

Community
  • 1
  • 1
madzohan
  • 11,488
  • 9
  • 40
  • 67