Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Exception raised when calling "save()" on simple model object. [RSS Feed]

#1 Oct. 25, 2005 17:22:47

Peter H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Exception raised when calling "save()" on simple model object.


Using Django revision 1011, I defined an app called "audit". I defined
a model for that app as follows:

class StatusType(meta.Model):
description = meta.CharField(maxlength=100)

...I tried testing it like this:

>>> from django.models.audit import *
>>> s = statustypes.StatusType()
>>> s.description = "foo"
>>> s.save()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/home/<user>/django/trunk/django/utils/functional.py", line 3,
in _curried
return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
File "/home/<user>/django/trunk/django/core/meta/__init__.py", line
783, in method_save
pk_val = getattr(self, opts.pk.column)
AttributeError: 'StatusType' object has no attribute 'id'

...given that I'm running on PostgreSQL and that the "id" field is
automatically created as a serial object and a primary key by Django, I
would expect it to be auto-populated at some point during or before the
call to "save()". I can define "id" by hand, but then that defeats the
whole point of having a serial object (which Django created on my
behalf in the first place). Is this a bug? Have I done something wrong?

TIA,
Pete

Offline

#2 Oct. 25, 2005 19:09:10

A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Exception raised when calling "save()" on simple model object.


I wrote a ticket about this (http://code.djangoproject.com/ticket/662).
I think I found where and why this occurs, but I don't have a patch for
it, because I suspect there are more things going on than I know about,
and I don't want to do just a quick fix for my particular issue.

Offline

#3 Oct. 25, 2005 19:29:31

A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Exception raised when calling "save()" on simple model object.


I forgot to add...in your particular case, I think

s = statustypes.StatusType(description='foo')

will work. Or if you don't know what the description is yet, and need
to save before setting it,

s = statustypes.StatusType(description='')

What it requires is having some kind of keyword argument. That triggers
the code that causes the AutoField (id) to be initialized.

Offline

#4 Oct. 25, 2005 19:35:02

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Exception raised when calling "save()" on simple model object.


Adam wrote:
> I wrote a ticket about this (http://code.djangoproject.com/ticket/662).
> I think I found where and why this occurs, but I don't have a patch for
> it, because I suspect there are more things going on than I know about,
> and I don't want to do just a quick fix for my particular issue.

Interesting. I started looking through the code to figure this out, but
then ran up against some other stuff and didn't get back to it. Anyway,
I tried providing kwargs as you suggested in the ticket, and that seems
to be a good work-around. Thanks for the pointer. =]

Pete

Offline

  • Root
  • » Django
  • » Exception raised when calling "save()" on simple model object. [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support