Django

Code

Ticket #163 (closed: fixed)

Opened 3 years ago

Last modified 1 year ago

Option to leave off __exact

Reported by: Manuzhai Assigned to: adrian
Milestone: Component: Template system
Version: magic-removal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description (Last modified by adrian)

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.

Attachments

Change History

07/22/05 13:00:29 changed by Manuzhai

Okay, so I meant

id__exact

07/22/05 13:01:30 changed by adrian

  • description changed.

07/22/05 13:01:57 changed by adrian

My thoughts on this: Explicit is better than implicit.

07/22/05 14:48:03 changed by rmunn@pobox.com

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. :-)

07/22/05 14:50:04 changed by rmunn@pobox.com

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. :-)

07/22/05 15:49:39 changed by rmunn@pobox.com

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).

07/22/05 16:21:01 changed by jacob

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.

07/22/05 23:04:37 changed by rmunn@pobox.com

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.

07/24/05 00:05:37 changed by Clint Ecker <clintecker@gmail.com>

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 :)

07/26/05 10:26:04 changed by adrian

  • status changed from new to assigned.

07/26/05 11:11:50 changed by adrian

  • status changed from assigned to closed.
  • resolution set to fixed.

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

07/03/06 02:10:57 changed by anonymous

  • severity changed from normal to major.
  • component changed from Admin interface to Template system.
  • priority changed from normal to low.
  • version set to magic-removal.
  • milestone set to Version 1.0.
  • type changed from defect to enhancement.

01/17/07 16:12:17 changed by

  • milestone deleted.

Milestone Version 1.0 deleted


Add/Change #163 (Option to leave off __exact)




Change Properties
Action