Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#107 closed defect (fixed)

[patch] Leaving out ID parameters

Reported by: Manuzhai <mail@…> Owned by: adrian
Component: Metasystem Version:
Severity: normal Keywords:
Cc: rmunn@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Change History (4)

comment:1 Changed 10 years ago by rmunn@…

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?

comment:2 Changed 10 years ago by rmunn@…

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.

comment:3 Changed 10 years ago by rmunn@…

  • Cc rmunn@… added
  • Summary changed from Leaving out ID parameters to [patch] Leaving out ID parameters

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

comment:4 Changed 10 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

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