Tuesday, July 24, 2012

Re: object has no attribute '_state'

Thank you!  Just also had this problem today, 24 July 2012.

Resolved now.  :)

Cheers!

On Thursday, July 12, 2012 10:30:27 AM UTC-7, aria wrote:
Hello, 

I realize this post is from 2010, but I also had this problem as of July 12 2012. It's still one of the first results in a search for the error, so I'm posting for future googlers. 

According to the Django Model reference, you should avoid overriding the __init__ method when inheriting from Model, especially if the signature changes. They recommend to instead add a custom manager to handle this. 

On Monday, July 5, 2010 7:51:51 PM UTC-7, jcage 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 = Question(script = "How are you?", comment = "no comment", order =
1)


but an attempt to run q1.save() results in the following error :

>>> q1.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/django/db/models/base.py", line 435, in save
    self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/django/db/models/base.py", line 447, in
save_base
    using = using or router.db_for_write(self.__class__,
instance=self)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/django/db/utils.py", line 133, in _route_db
    return hints['instance']._state.db or DEFAULT_DB_ALIAS
AttributeError: 'Question' object has no attribute '_state'



The class definitions are :

class QuestionSet(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    order = models.IntegerField()

    def __init__(self, *args, **kwargs):
        self.title = kwargs.get('title','Default Title')
        self.description = kwargs.get('description', 'DefDescription')
        self.order = kwargs.get('order', 0)


class Question(QuestionSet):
    script = models.CharField(max_length=200)
    comment = models.TextField()


    def __init__(self, *args, **kwargs):
        QuestionSet.__init__(self, args, kwargs)
        self.script = kwargs.get('script', "undefined")
        self.comment = kwargs.get('comment', "no comment")




Would greatly appreciate any suggestions

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/SxjdrS-5MhMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment