Message from discussion
object has no attribute '_state'
Received: by 10.204.85.90 with SMTP id n26mr164758bkl.9.1278406350664;
Tue, 06 Jul 2010 01:52:30 -0700 (PDT)
X-BeenThere: django-users@googlegroups.com
Received: by 10.204.74.5 with SMTP id s5ls3737741bkj.2.p; Tue, 06 Jul 2010
01:50:11 -0700 (PDT)
Received: by 10.204.82.130 with SMTP id b2mr165020bkl.0.1278406211559;
Tue, 06 Jul 2010 01:50:11 -0700 (PDT)
Received: by 10.204.82.130 with SMTP id b2mr165019bkl.0.1278406211530;
Tue, 06 Jul 2010 01:50:11 -0700 (PDT)
Return-Path: <tevans...@googlemail.com>
Received: from mail-fx0-f53.google.com (mail-fx0-f53.google.com [209.85.161.53])
by gmr-mx.google.com with ESMTP id bv13si2916931bkb.0.2010.07.06.01.50.10;
Tue, 06 Jul 2010 01:50:10 -0700 (PDT)
Received-SPF: pass (google.com: domain of tevans...@googlemail.com designates 209.85.161.53 as permitted sender) client-ip=209.85.161.53;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of tevans...@googlemail.com designates 209.85.161.53 as permitted sender) smtp.mail=tevans...@googlemail.com; dkim=pass (test mode) header...@googlemail.com
Received: by mail-fx0-f53.google.com with SMTP id 19so4727652fxm.12
for <django-users@googlegroups.com>; Tue, 06 Jul 2010 01:50:10 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=googlemail.com; s=gamma;
h=domainkey-signature:mime-version:received:received:in-reply-to
:references:date:message-id:subject:from:to:content-type
:content-transfer-encoding;
bh=10Zl8nPIIvIYgIThNbmT0E51MgmSNXSR/Att9Uv8BFA=;
b=bD2khVWsbG9UmwdcJgVgZ47kSPmFUvdrfFnnQmYntqWljhimfa7doQWjiMN68KlBHy
cLSnFVUrMB2YkK/EYaanRp+7SBicjl5ZyS/MrZ97MmkJU/VuaL+HPLU+HqwcjRobqpig
rTx8PPwIPLlv7abn0usNughjNzCQto4BLFoko=
DomainKey-Signature: a=rsa-sha1; c=nofws;
d=googlemail.com; s=gamma;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type:content-transfer-encoding;
b=JdfIDlQz061y1uMxLiebEQYogBW4BFDORxxJyfM/YiyznYPDkzEtbNehZvma4dcxd0
QfJSapg1XsEp+ylmlarj3lYL4ITASlgbMBhZ2yL8KsbiS5CSdcVSUPwlFqyO73sjkXnd
myTg+xvngjiZvdbY6lhmURVL4XzfJB66NBns8=
MIME-Version: 1.0
Received: by 10.239.187.203 with SMTP id m11mr334250hbh.124.1278406210279;
Tue, 06 Jul 2010 01:50:10 -0700 (PDT)
Received: by 10.239.185.1 with HTTP; Tue, 6 Jul 2010 01:50:10 -0700 (PDT)
In-Reply-To: <1caf9be7-a687-4ebf-8ac8-14d6ac1ec...@d16g2000yqb.googlegroups.com>
References: <1caf9be7-a687-4ebf-8ac8-14d6ac1ec...@d16g2000yqb.googlegroups.com>
Date: Tue, 6 Jul 2010 09:50:10 +0100
Message-ID: <AANLkTikJ9pPJEV4G-wF7bYr1t5F9Rk6DXJidHAC6V...@mail.gmail.com>
Subject: Re: object has no attribute '_state'
From: Tom Evans <tevans...@googlemail.com>
To: django-users@googlegroups.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
On Tue, Jul 6, 2010 at 3:51 AM, jcage <jcage3...@gmail.com> wrote:
> Hi everyone. I'm quite new to python and django. I was wondering if
> anyone could shed some light on this topic :
>
> My models.py contains the classes QuestionSet and Question, which
> inherits the former.
> In the shell, I define a Question object as follows
>
> q1 =3D Question(script =3D "How are you?", comment =3D "no comment", orde=
r =3D
> 1)
>
>
> but an attempt to run q1.save() results in the following error :
>
>>>> q1.save()
> Traceback (most recent call last):
> =C2=A0File "<console>", line 1, in <module>
> =C2=A0File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/db/models/base.py", line 435, in save
> =C2=A0 =C2=A0self.save_base(using=3Dusing, force_insert=3Dforce_insert,
> force_update=3Dforce_update)
> =C2=A0File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/db/models/base.py", line 447, in
> save_base
> =C2=A0 =C2=A0using =3D using or router.db_for_write(self.__class__,
> instance=3Dself)
> =C2=A0File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/django/db/utils.py", line 133, in _route_db
> =C2=A0 =C2=A0return hints['instance']._state.db or DEFAULT_DB_ALIAS
> AttributeError: 'Question' object has no attribute '_state'
>
>
>
> The class definitions are :
>
> class QuestionSet(models.Model):
> =C2=A0 =C2=A0title =3D models.CharField(max_length=3D100)
> =C2=A0 =C2=A0description =3D models.TextField()
> =C2=A0 =C2=A0order =3D models.IntegerField()
>
> =C2=A0 =C2=A0def __init__(self, *args, **kwargs):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0self.title =3D kwargs.get('title','Default Tit=
le')
> =C2=A0 =C2=A0 =C2=A0 =C2=A0self.description =3D kwargs.get('description',=
'DefDescription')
> =C2=A0 =C2=A0 =C2=A0 =C2=A0self.order =3D kwargs.get('order', 0)
>
>
> class Question(QuestionSet):
> =C2=A0 =C2=A0script =3D models.CharField(max_length=3D200)
> =C2=A0 =C2=A0comment =3D models.TextField()
>
>
> =C2=A0 =C2=A0def __init__(self, *args, **kwargs):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0QuestionSet.__init__(self, args, kwargs)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0self.script =3D kwargs.get('script', "undefine=
d")
> =C2=A0 =C2=A0 =C2=A0 =C2=A0self.comment =3D kwargs.get('comment', "no com=
ment")
>
>
>
>
> Would greatly appreciate any suggestions
>
You aren't calling the django.db.models.Model constructor on either of
your derived classes. When you write a derived class, be sure to call
the super class constructor:
class MyModel(Model):
def __init__(self, *args, **kwargs):
super(MyModel, self).__init__(*args, **kwargs)
Cheers
Tom