Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#163 closed enhancement (fixed)

Option to leave off __exact

Reported by: Manuzhai Owned by: Adrian Holovaty
Component: Template system Version: magic-removal
Severity: major Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adrian Holovaty)

Doing sites.get_object(id__exact = 1) seems kind of verbose if sites.get_object(id = 1) would convey the same meaning and be a little easier to read.

Change History (13)

comment:1 by Manuzhai, 19 years ago

Okay, so I meant

id__exact

comment:2 by Adrian Holovaty, 19 years ago

Description: modified (diff)

comment:3 by Adrian Holovaty, 19 years ago

My thoughts on this: Explicit is better than implicit.

comment:4 by rmunn@…, 19 years ago

But not if what's implicit is blindingly obvious.

The option of idexact should be kept, but I can't conceive of any other meaning for polls.get_object(id=5) than id__exact=5. Beautiful is better than ugly. Simple is better than complex. Readability counts. I'll see your Zen and raise you two more. :-)

comment:5 by rmunn@…, 19 years ago

Argh, should have put triple-braces around that id__exact. What I meant to say was:

The option of id__exact should be kept, but I can't conceive of any other meaning for polls.get_object(id=5) than id__exact=5.

Beautiful is better than ugly.
Simple is better than complex.
Readability counts.

I'll see your Zen quote and raise you two more. :-)

comment:6 by rmunn@…, 19 years ago

Besides, look at how short the patch is!

Index: django/core/meta.py
===================================================================
--- django/core/meta.py	(revision 299)
+++ django/core/meta.py	(working copy)
@@ -1142,8 +1147,10 @@
             continue
         lookup_list = kwarg.split(LOOKUP_SEPARATOR)
         if len(lookup_list) == 1:
-            _throw_bad_kwarg_error(kwarg)
-        lookup_type = lookup_list.pop()
+            # polls.get_object(id=5) is shorthand for polls.get_object(id__exact=5)
+            lookup_type = 'exact'
+        else:
+            lookup_type = lookup_list.pop()
         current_opts = opts # We'll be overwriting this, so keep a reference to the original opts.
         current_table_alias = current_opts.db_table
         param_required = False

Isn't it the cutest patch you've ever seen? It followed me home, Mom, can I please apply it to the repository? :-)

On a serious note: this will only work for lookups that are one "level" of complexity deep, e.g. get_object(id=5). I thought about the possibility of doing choices.get_list(polls__sites__id=5) instead of choices.get_list(polls__sites__id__exact=5) and realized no, that wouldn't be a good idea. If you're writing out a relation of this much complexity, it's better to be explicit about the fact that you're doing an exact match. But in the vast majority of cases where you'd want to use the shorthand, you're doing something like get_object(id=5).

comment:7 by Jacob, 19 years ago

See the discussion on this ticket here: http://loglibrary.com/show_page/view/179?Multiplier=3600&Interval=6&StartTime=1122065878 and the resulting proposal here: http://groups-beta.google.com/group/django-developers/browse_thread/thread/6eb17e5d996ed393/ab1cee3ad60980a7#ab1cee3ad60980a7

Basically: add a magic "pk=" keyword to lookups that would do a "primarykeyfieldexact=whatever" lookup for you.

comment:8 by rmunn@…, 19 years ago

Here's a patch to use the magic "pk=(value)" syntax.

Index: django/core/meta.py
===================================================================
--- django/core/meta.py (revision 300)
+++ django/core/meta.py (working copy)
@@ -1142,7 +1147,12 @@
             continue
         lookup_list = kwarg.split(LOOKUP_SEPARATOR)
         if len(lookup_list) == 1:I tested against SQLite;
-            _throw_bad_kwarg_error(kwarg)
+            if kwarg == 'pk':
+                # "pk=value" is shorthand for "(primary key)__exact=value"
+                lookup_list = [opts.pk.name, 'exact']
+            else:
+                # Not shorthand, just a bad kwarg
+                _throw_bad_kwarg_error(kwarg)
         lookup_type = lookup_list.pop()
         current_opts = opts # We'll be overwriting this, so keep a reference to the original opts.
         current_table_alias = current_opts.db_table

Again, it would be possible to do this at deeper levels, but I think the risk of ambiguity isn't worth the effort saved. Besides, if you're going through one table to get to the next table, you probably don't have an id into the second table anyway.

comment:9 by Clint Ecker <clintecker@…>, 19 years ago

I would like it if sites.get_object(1) would default to using the primary key. I personally think id= is a little verbose myself :)

comment:10 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:11 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

(In [316]) Fixed #163 -- Added 'pk' database API option, which is a shorthand for (primary_key)exact

comment:12 by anonymous, 18 years ago

Component: Admin interfaceTemplate system
milestone: Version 1.0
priority: normallow
Severity: normalmajor
Type: defectenhancement
Version: magic-removal

comment:13 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top