Django

Code

Ticket #107 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

[patch] Leaving out ID parameters

Reported by: Manuzhai <mail@manuzhai.nl> Assigned to: adrian
Milestone: Component: Metasystem
Version: Keywords:
Cc: rmunn@pobox.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

For models with an AutoField?, it should be possible when creating a new instance (for example in the interactive interpreter) to leave out the id = None parameter. Django should be smart enough to add this by itself.

Attachments

Change History

07/21/05 09:07:44 changed by rmunn@pobox.com

I concur; I'm just now going through the tutorial, and having to do p = polls.Poll(id=None, question="What's up?", pub_date=datetime.now())" bugged me. I should be able to do p = polls.Poll(question="What's up?", pub_date=datetime.now()) instead. I didn't create the id field, so why should I have to remember that it's there when I'm creating a new object?

07/21/05 10:07:54 changed by rmunn@pobox.com

The following patch will solve this: it ensures that the primary key gets set to None even if the user omits setting the primary key in the arguments to __init__:

Index: core/meta.py
===================================================================
--- core/meta.py        (revision 271)
+++ core/meta.py        (working copy)
@@ -684,6 +684,8 @@
 # CORE METHODS #############################

 def method_init(opts, self, *args, **kwargs):
+    # Ensure primary key gets set even if user omits it from args and kwargs
+    setattr(self, opts.pk.name, None)
     for i, arg in enumerate(args):
         setattr(self, opts.fields[i].name, arg)
     for k, v in kwargs.items():

Or you could instead apply the following patch, which will set all AutoFields? (not just the primary key) to None.

Index: core/meta.py
===================================================================
--- core/meta.py        (revision 271)
+++ core/meta.py        (working copy)
@@ -684,6 +684,11 @@
 # CORE METHODS #############################

 def method_init(opts, self, *args, **kwargs):
+    # Ensure any AutoFields get assigned some value even if user omits them
+    # from args and kwargs
+    for field in opts.fields:
+        if isinstance(field, AutoField):
+            setattr(self, field.name, None)
     for i, arg in enumerate(args):
         setattr(self, opts.fields[i].name, arg)
     for k, v in kwargs.items():

In both patches, the user's preferred values for the field will override the None.

Caution: I have tested these patches briefly, and they appear to work. They let me do p = polls.Poll(question="What's up?", pub_date=datetime.now()) and the id gets auto-set the way I expect. But I do not know the code well enough to test this in detail; I don't know what other bugs it might introduce. The second patch in particular might have far-reaching implications if AutoFields? are widely used throughout the code.

But either one of these two patches would solve this problem and let me forget about the id field until I actually need it.

07/29/05 11:57:11 changed by rmunn@pobox.com

  • cc set to rmunn@pobox.com.
  • summary changed from Leaving out ID parameters to [patch] Leaving out ID parameters.

Added "patch" to ticket summary to flag it for developers' attention. :-)

08/01/05 11:26:40 changed by adrian

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

(In [360]) Fixed #239 and #107 -- Changed model init() to use Field.get_default() if the value wasn't explicitly passed as a keyword argument. That means setting 'id=None' is no longer necessary, and you can leave off fields if you want them to have default values set.


Add/Change #107 ([patch] Leaving out ID parameters)




Change Properties
Action