1

I'm trying to get a single row from my database using the "get" method in Python/Django. The model I'm using is

class SavedQuery(models.Model):
    name = models.CharField(max_length = 512)
    queryText = models.CharField(max_length = 2048)
    created = models.DateTimeField(auto_now_add=True)

Now, I've saved some values in that database, ensuring that all of them are unique. So now I want to get the value of the queryText when given a name. I tried using the following code:

    entry = SavedQuery.objects.get(name = query_name) 
    #query_name is an input to the function, and I've verified it is correct

The line for entry, however, does not work. I'm getting a 500 error through the browser, and the terminal I'm running the server from isn't showing any error message. The DEBUG output is incomprehensible to me as well.

My band-aid solution is to use SavedQuery.objects.all() and then go through and check the name field, but I have seen on the Django site that it's possible to get a list of objects with a certain value in a given field, so I'd rather just do that.

EDIT: Debugging output from the spot where it's messing up is

2014-06-12 19:56:20 DEBUG    bolt django.db.backends util.execute:79: (0.000) SELECT `bolt_savedquery`.`id`, `bolt_savedquery`.`name`, `bolt_savedquery`.`queryText`, `bolt_savedquery`.`created` FROM `bolt_savedquery` WHERE `bolt_savedquery`.`name` = 'Kyle' ; args=(u'Kyle',)
2014-06-12 19:56:20 DEBUG    bolt django.db.backends util.execute:79: (0.000) SELECT `bolt_savedquery`.`id`, `bolt_savedquery`.`name`, `bolt_savedquery`.`queryText`, `bolt_savedquery`.`created` FROM `bolt_savedquery` LIMIT 21; args=()
5
  • 1
    There has to be some error. Can't help without traceback or error. Does .get raise MultipleObjectsReturned or ObjectDoesNotExist or something? Commented Jun 12, 2014 at 20:02
  • Additionally, during development, I encourage you to set DEBUG=True in your settings.py file. That way you will see tracebacks in the browser rather than a 500 error... Commented Jun 12, 2014 at 20:05
  • I had DEBUG enabled, but then I couldn't even find my print debugging within all the other debugging output, and it didn't make sense to me, so I wasn't using it. Commented Jun 12, 2014 at 20:11
  • 1
    And what is displayed on the webpage when the error occured? You shull have a full page with the exception raised and a complete traceback. Commented Jun 12, 2014 at 20:31
  • I get multiple objects returned. I think I accidentally had a duplicate in there during testing, so perhaps that is why. Commented Jun 12, 2014 at 20:36

1 Answer 1

3

As per your comments, you're having a SavedQuery.MultipleObjectsReturned error raised. You'll need to either make the name field a unique field so you know you'll only get one result back when using SavedQuery.objects.get() or you can do something like SavedQuery.objects.filter(name=VALUE).first(). You could also use .last() to get the most recently created one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.