Opened 19 years ago

Closed 19 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

Description

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()
>>> p.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\progra~1\python23\lib\site-packages\django\utils\functional.py", line
 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.item
s()))
  File "c:\progra~1\python23\lib\site-packages\django\core\meta\__init__.py", li
ne 783, in method_save
    pk_val = getattr(self, opts.pk.column)
AttributeError: 'Poll' object has no attribute 'id'
>>> c = companies.Company(name='something')
>>> c.save()
>>> (no error)

I think the problem (if this is considered a problem) comes in django/core/meta/__init__.py 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 by Adam Endicott <leftwing17@…>, 19 years ago

Component: Admin interfaceCore framework

comment:2 by Adam Endicott <leftwing17@…>, 19 years ago

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 by Tekhne, 19 years ago

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 by Adrian Holovaty, 19 years ago

#712 is a duplicate.

comment:5 by levicook@…, 19 years ago

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/__init__.py
Index: django/core/meta/__init__.py
===================================================================
--- django/core/meta/__init__.py        (revision 1032)
+++ django/core/meta/__init__.py        (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, opts.pk.column)
+    pk_val = None
+    if hasattr(self, opts.pk.column):
+        pk_val = getattr(self, opts.pk.column)
     pk_set = bool(pk_val)
     record_exists = True
     if pk_set:

comment:6 by Adrian Holovaty, 19 years ago

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