django - A model with a defined field throws an AttributeError when accessing it -


i have django 1.5 app custom user model. has following field (among bunch of others):

class user(models.model):     ...      reporting_period = models.charfield(         max_length=20,         choices=reporting_periods,         null=true,         blank=true,         default=none     )      company = models.foreignkey(         company,         null=true,         blank=true,         default=none     ) 

this model has following function defined:

def get_reporting_period(self, company_reporting_period=none):     if not self.reporting_period:         if not hasattr(self, '_company_reporting_period'):             if company_reporting_period:                 self._company_reporting_period = company_reporting_period             else:                 self._company_reporting_period = self.company.reporting_period         return self._company_reporting_period     else:         return self.reporting_period 

occasionally, get_reporting_period function throws attributeerror ('user' object has no attribute 'reporting_period') on line if not self.reporting_period:. scratching head how possible.

self user instance, , therefore should have reporting_period @ times. there times during model instance's lifetime in django when field not accessible property?

unfortunately, issue happens in production environment unable debug it.

field attributes on instances not defined until __init__ method of model class called. trying access field attribute in custom __init__ method before calling super method result in attributeerror.

otherwise must've accidentally deleted attribute on instance.

i believe 2 instances when field attribute not defined on model instance.

edit: i've done small test, , unpickling preserves attributes of original version of model, new methods carry on recreated model. able call new get_reporting_period function, raise attributeerror when trying access undefined reporting_period attribute.

that means in case should invalidate cached entries after adding new field. however, there plenty of serialization methods not result in such error. take @ answer this question way serialize models without problem.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -