Opened 13 years ago

Closed 13 years ago

#662 closed defect (fixed)

saving a newly created object without any data causes an error

Reported by: Adam Endicott <leftwing17@…> Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This is best explained with an example:

class Poll(meta.Model):
    name = meta.CharField(maxlength=100, blank=True)
>>> from django.models.myProject import polls
>>> p = polls.Poll()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\progra~1\python23\lib\site-packages\django\utils\", line
 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.item
  File "c:\progra~1\python23\lib\site-packages\django\core\meta\", li
ne 783, in method_save
    pk_val = getattr(self,
AttributeError: 'Poll' object has no attribute 'id'
>>> c = companies.Company(name='something')
>>> (no error)

I think the problem (if this is considered a problem) comes in django/core/meta/ in the method_init method. If kwargs are supplied to the __init__, every field in the object is set to some value, including the id AutoField. If there are no kwargs, the id field isn't set, and you get an error in save(). I don't know the best way to fix this. Removing the if kwargs check at the start of method_init gets rid of the error, but its probably not what's desired. Maybe an explicit check at the end that id (or any AutoFields) have been set?

Change History (6)

comment:1 Changed 13 years ago by Adam Endicott <leftwing17@…>

Component: Admin interfaceCore framework

comment:2 Changed 13 years ago by Adam Endicott <leftwing17@…>

And before anyone asks, I'm not actually having a problem with this because I'm trying to create data-less objects. My actual problem comes with objects that have only a FileField and a DateTime (auto_now=True) field. I'm creating them with f = files.File() then trying to call f.save_file_file(filename, content), and that's where I get the error.

comment:3 Changed 13 years ago by Tekhne

I'm also experiencing this problem where I attempt to save model objects for which I have not provided key word arguments at model instantiation time, and where I have not explicitly set the implicitly created primary key field "id".

comment:4 Changed 13 years ago by Adrian Holovaty

#712 is a duplicate.

comment:5 Changed 13 years ago by levicook@…

To keep rolling, I added a simple patch to my working copy. I haven't looked at the code to enough to know if the approach my patch takes is sensible, but it works.

svn diff

levi@id ~/Projects/3rdParty/django_src
$ svn diff django/core/meta/
Index: django/core/meta/
--- django/core/meta/        (revision 1032)
+++ django/core/meta/        (working copy)
@@ -780,7 +780,9 @@
     cursor = db.db.cursor()
     # First, try an UPDATE. If that doesn't update anything, do an INSERT.
-    pk_val = getattr(self,
+    pk_val = None
+    if hasattr(self,
+        pk_val = getattr(self,
     pk_set = bool(pk_val)
     record_exists = True
     if pk_set:

comment:6 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

I think this is fixed now. Please reopen if it's still an issue.

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