Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django .get() raises DoesNotExist error without explicit '__exact' lookup

Tags:

orm

django

I've faced with issue, which I cannot neither understand nor 'google', while followed steps from "Practical Django Projects" book.

The example of view code below should return specific Entry object (I omitted unnecessary part), but it raises "DoesNotExist: Entry matching query does not exist" error:

...
return render_to_response('weblog/entry_detail.html',
                          {'entry':Entry.objects.get(pub_date__year=pub_date.year,
                                           pub_date__month=pub_date.month,
                                           pub_date__day=pub_date.day,
                                           slug=slug)

(I confirmed that target object indeed exists, etc.)

When I replaced .get() with filter() method, it returned me queryset with my target object.

After some 'hitting-the-wall' sessions, I managed to make .get() call work when replaced

   slug=slug

with

   slug__exact=slug

I cannot get the difference between these two. Seems to me, Django's docs clearly state that in such case '__exact' is implied (Django 1.6, "Making queries")

I also cannot check the actual SQL query which Django ran in the .get() cases to compare with SQL query used with .filter() method (the result is either object, not queryset, or raised exception).

So, I actually have 2 workarounds (filter()[0] to get single object, or '__exact'), but I want to understand the weird behavior with .get() method.

My questions are:

  1. Did I misunderstand documentation about '__exact' implication in my case?
  2. If no, isn't my problem a Django/DB bug?
  3. Is there a way to check the actual SQL query which Django has performed, when result of query is not a queryset object?

Thank you!

Note: I run Django 1.6.1 / Python 2.7.3 / MySQL 5.5.33

Update: I installed suggested django-debug-toolbar and was able to compare queries for .get() call with & w/o '__exact' lookup. The only difference that I see between these queries, is the order of 'AND' conditions, so still have no clue what's going on:

  1. slug=slug:

    SELECT weblog_entry.id, weblog_entry.title, weblog_entry.excerpt, weblog_entry.body, weblog_entry.pub_date, weblog_entry.excerpt_html, weblog_entry.body_html, weblog_entry.author_id, weblog_entry.slug, weblog_entry.status, weblog_entry.enable_comments, weblog_entry.featured FROM weblog_entry WHERE (EXTRACT(MONTH FROM CONVERT_TZ(weblog_entry.pub_date, 'UTC', 'UTC')) = 2 AND weblog_entry.pub_date BETWEEN '2014-01-01 00:00:00' and '2014-12-31 23:59:59' AND EXTRACT(DAY FROM CONVERT_TZ(weblog_entry.pub_date, 'UTC', 'UTC')) = 2 AND weblog_entry.slug = '3rd-entry' )

  2. slug__exact=slug:

    SELECT weblog_entry.id, weblog_entry.title, weblog_entry.excerpt, weblog_entry.body, weblog_entry.pub_date, weblog_entry.excerpt_html, weblog_entry.body_html, weblog_entry.author_id, weblog_entry.slug, weblog_entry.status, weblog_entry.enable_comments, weblog_entry.featured FROM weblog_entry WHERE (EXTRACT(MONTH FROM CONVERT_TZ(weblog_entry.pub_date, 'UTC', 'UTC')) = 2 AND weblog_entry.pub_date BETWEEN '2014-01-01 00:00:00' and '2014-12-31 23:59:59' AND weblog_entry.slug = '3rd-entry' AND EXTRACT(DAY FROM CONVERT_TZ(weblog_entry.pub_date, 'UTC', 'UTC')) = 2)

Note: I've tried to execute these queries manually from mysql console and they both selected target entry;

Update2: I've changed title to point on the problem more precisely.

like image 284
Val Avatar asked Dec 01 '25 01:12

Val


1 Answers

Ok, let me speak for collective unconscious.

Did I misunderstand documentation about '__exact' implication in my case?

No, you got it right. Let me quote django doc:

As a convenience when no lookup type is provided (like in Entry.objects.get(id=14)) the lookup type is assumed to be exact.

And since you observe something that contradicts requirements, answer to the next question is:

If no, isn't my problem a Django/DB bug?

Yes, it is a bug. Me and whole Django community will be grateful if you describe steps to reproduce and file an issue

Is there a way to check the actual SQL query which Django has performed, when result of query is not a queryset object?

Looks you already got the answer :)

like image 199
Marat Avatar answered Dec 03 '25 19:12

Marat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!