Django "can't set attribute" in model
<p>I have a model like this one:</p>

<pre><code>class MyReport(models.Model):
    group_id    = models.PositiveIntegerField(blank=False, null=False)
    test        = models.ForeignKey(Test, on_delete=models.CASCADE)
    owner       = models.ForeignKey(User, editable=False, default=get_current_user, on_delete=models.CASCADE)

    user_objects = UserFilterManager()

    @property
    def location(self):
        return self.test.location
</code></pre>

<p>to which I added a <em>location</em> property.
I get this error at run-time. I pasted only parts below my latest call, which was to len() in this case (I know, use <em>count</em> instead).</p>

<pre><code>  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 240, in __len__
    self._fetch_all()
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 75, in __iter__
    setattr(obj, attr_name, row[col_pos])
AttributeError: can't set attribute
</code></pre>

<p>I cannot figure why the error? If I call it <em>locationx</em> then there is no error.</p>

<p>Are there some special property definition rules for Django?</p>

<p>How could I debug this?</p>

<p>I added the property since I added the <em>test</em> indirection and want to change as little code as possible. I could, of course, just change each <code>report.location</code> to <code>report.test.location</code>, but that's a nuisance. It would be great if I could define an alias for DB searches so I would need to change those either.</p>

<p><strong>EDIT</strong>: To answer the comments:</p>

<ol>
<li>I do not want to set the value, I just want a property (getter). I want to be able to do <code>report.location</code> instead adding an indirection <code>report.test.location</code> (don't want to change existing code where location was inside the report model).</li>
<li><code>self.test.location</code> and <code>report.test.location</code> work, and also <code>reports.locationx</code> works as well.</li>
<li>I mentioned this above, but I will repeat. All I did was to call len() on a query, e.g. <code>len(MyReport.objects.all())</code>. </li>
</ol>

<p>Here is a full trace from the Eclipse console:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Eclipse-SDK-4.2.1-win32-x86_64\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd_comm.py", line 765, in doIt
    result = pydevd_vars.evaluateExpression(self.thread_id, self.frame_id, self.expression, self.doExec)
  File "C:\Eclipse-SDK-4.2.1-win32-x86_64\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd_vars.py", line 378, in evaluateExpression
    sys.stdout.write('%s\n' % (result,))
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 234, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 258, in __iter__
    self._fetch_all()
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 75, in __iter__
    setattr(obj, attr_name, row[col_pos])
AttributeError: can't set attribute
</code></pre>