I have a Django 1.5 app with a custom User
model. It has the following field (among a 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 also has the 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, the get_reporting_period
function throws an AttributeError ('User' object has no attribute 'reporting_period') on the line if not self.reporting_period:
. I am scratching my head as to how that is possible.
self
is a User
instance, and therefore should have a reporting_period
at all times. Are there any times during a model instance's lifetime in Django when a field is not accessible as a property?
Unfortunately, the issue only happens in my production environment where I am unable to debug it.