In django, __init__ function on Model, causes attribute error, cannot save to database

I am using django and a model definition such as

class Question(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)

Attempting to call save() on an object of the question class, causes the shell to respond with

/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'

However, removing the __init__ function, makes everything ok again. Any idea on what causes this and how to resolve it?

many thanks

-------------Problems Reply------------

You need to call the superclass' __init__ method at some point in your subclass' __init__ method:

def __init__(self, *args, **kwargs):
super(Question, self).__init__(*args, **kwargs)
# your code here

According to the Django models docs, it's not recommended to overwrite __init__ method for models. Using @classmethod or custom manager is preferred.

Category:django Views:7 Time:2010-07-06
Tags: django

Related post

  • How i can refresh model's attributes when invoke save using backbone 2011-08-19

    How i can refresh model's attributes when invoke save() I am using backbone and backbone relational. Have the following code: saveParams: function(event){ var self = this; this.model.save({}, { success: function(model, resp, xhr){ model = ... }, erro

  • Importing view functions to models.py - triggering via override save method 2011-03-25

    I would like to trigger a view function from within the save method of a model, in order to write or update an xml file associated with the instance. #models.py from myapp.views import updateXML, createXML class myModel(models.Model): def save(self,

  • django - how to get model objects from another process instead of database 2011-03-09

    I have a TCP server, a Python app, that gets the information from the connected devices. This information I am trying to show using Django web app. So, basically my model objects reside within the TCP server process. I can modify the TCP server to pu

  • Rails 3 Nested Models unknown attribute Error 2011-07-27

    I have a model "Issue" and a nested Model "Relationship" In the issue.rb I have mentioned: has_many :relationships, :dependent => :destroy accepts_nested_attributes_for :relationships, :allow_destroy => true In relationship.rb I have mentioned:

  • Python Django ModelForm has no model class specified error 2012-02-21

    I want to write an inventory manager. i written a django app before and in the first app everything works fine, but now i got a problem. when i try to render the template i get the error: ModelForm has no model class specified - i looked up some foru

  • ActiveRecord nested attributes, assignment auto-saves to database 2012-01-02

    Is there a way to assign value to a specific attribute in a model which is declared as a nested attribute without instantly saving it in the database? (e.g. keep it in the memory first) --------------Solutions------------- Quotied from => http://a

  • Writing a __init__ function to be used in django model 2009-05-09

    I'm trying to write an __init__ function for one of my models so that I can create an object by doing p = User('name','email') When I write the model, I have def __init__(self, name, email, house_id, password): models.Model.__init__(self) self.name =

  • In Django - Model Inheritance - Does it allow you to override a parent model's attribute? 2010-02-26

    I'm looking to do this: class Place(models.Model): name = models.CharField(max_length=20) rating = models.DecimalField() class LongNamedRestaurant(Place): # Subclassing `Place`. name = models.CharField(max_length=255) # Notice, I'm overriding `Place.

  • Django Rest Framework: 'function' object has no attribute 'as_view' 2011-07-27

    I've been trying for a while to get a ModelResource or a View working using the Django Rest Framework. I'm following the examples but the code in the examples is not working for me. Can anyone tell me why I might be getting this error. views.py # Cre

  • Python, Django 'function' object has no attribute 'all' 2012-01-21

    Hi I got this problem when accessing database: AttributeError at /renderForm/0 'function' object has no attribute 'all' Request Method: GET Request URL: http://127.0.0.1:8000/renderForm/0 Django Version: 1.2.5 Exception Type: AttributeError Exception

  • Django 'function' object has no attribute 'objects' 2013-03-16

    My apps allow you to like a picture then it redirects you to the same page I get then error when I try to like a picture I can create a like objects with the shell prompt but why I get this error? thank for helping me AttributeError at /like/3/ funct

  • Where and how is django Model objects attribute defined? 2009-07-20

    I'm trying to get my head around Django ORM. I've been reading django.db.models.base.py source code but still could understand how does the Model.objects attributes in our Model object gets defined. Does anybody know how does django adds that objects

  • 'function' object has no attribute 'key' when twitter user login my site ,( my site using django) 2011-01-01

    i follow this article : http://hameedullah.com/step-by-step-guide-to-use-sign-in-with-twitter-with-django.html to login with twitter . and this is my callback method of django twitter login : def callback(req): token = req.session.get('token', None)

  • Django attribute error: 'module' object has no attribute 'is_usable' 2010-05-31

    I got the following error when calling the url in Django. It's working before, I guess it's related with some accidental changes I made, but I have no idea what they are. Thanks before for the help, Robert Environment: Request Method: GET Request URL

  • Django manager attribute error 2010-09-26

    I have a problem to add custom methods to my models. I found solution in django book but it does not work. Here is my code for models class NewsManager(models.Manager): def getLastNews(self): return self.objects.order_by('-id')[:3] class News(models.

  • Django and Ajax based model saving 2010-12-16

    via ajax, I want to post some data and if model successfully gets saved, returns answer as a JSON object. Here is my jquery based ajax post: var requestData = { 'ievent_id': type , 'channel_id': CHANNEL_ID , 'start_date': dateToStrConverter(start_dat

  • Are Mixin class __init__ functions not automatically called in python? 2011-05-23

    I'd like to use a Mixin to always add some init functionality to my child classes which each inherit from different API base classes. Specifically, I'd like to make multiple different child classes that inherit from one of these different API-supplie

  • Django admin - 'NoneType' object has no attribute 'rindex' 2011-06-09

    I'm working through the Django 1.0 Website Development book. At Chapter 8, I can't get the admin application to work. Here is the traceback I'm getting. Environment: Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Django Version: 1.3 Py

  • django pluralization functional.__proxy__object instead of verbose_name 2009-12-30

    I`m trying to create a message which uses some kind of pluralization. The message look like this and depends on the number of deleted objects. Successfully deleted [number of objects] Contact(s) Thus output can be: Successfully deleted 1 Contact Succ

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.196 (s). 11 q(s)