#107 closed defect (fixed)
[patch] Leaving out ID parameters
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
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: | no | UI/UX: | no |
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 by , 19 years ago
comment:2 by , 19 years ago
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 by , 19 years ago
Cc: | added |
---|---|
Summary: | Leaving out ID parameters → [patch] Leaving out ID parameters |
Added "patch" to ticket summary to flag it for developers' attention. :-)
comment:4 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 dop = polls.Poll(question="What's up?", pub_date=datetime.now())
instead. I didn't create theid
field, so why should I have to remember that it's there when I'm creating a new object?