Bug? - model with property + objects.values()

11 views
Skip to first unread message

Jens Diemer

unread,
Nov 14, 2007, 4:15:17 AM11/14/07
to django...@googlegroups.com

I would like to escape the data from a model attribute...

Here a example:

Old model:
-------------------------------------------------------------
class Page(models.Model):
...
name = models.CharField()
...
-------------------------------------------------------------


I have changed the model to this:
-------------------------------------------------------------
class Page(models.Model):
...
__name = models.CharField(
db_column='name'
)
def __get_name(self):
return escape(self.__name)

def __set_name(self, data):
self.__name = data

name = property(__get_name, __set_name)
...
-------------------------------------------------------------

Now, i have a problem :(

This works, fine:
page = Page.objects.all()[0]
print page.name


But here i get a 'FieldDoesNotExist' Traceback:
print Page.objects.values("name")

The complete traceback:
-------------------------------------------------------------
Traceback (most recent call last):
File "./PyLucid_shortcuts_test.py", line 24, in <module>
print Page.objects.values("name")
File "./django/db/models/query.py", line 108, in __repr__
return repr(self._get_data())
File "./django/db/models/query.py", line 482, in _get_data
self._result_cache = list(self.iterator())
File "./django/db/models/query.py", line 597, in iterator
fields = [self.model._meta.get_field(f, many_to_many=False) for f in
self._fields]
File "./django/db/models/options.py", line 131, in get_field
raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name)
django.db.models.fields.FieldDoesNotExist: Page has no field named 'name'
-------------------------------------------------------------


Why?
Is this a bug?

--
Mfg.

Jens Diemer


----
A django powered CMS: http://www.pylucid.org

Malcolm Tredinnick

unread,
Nov 14, 2007, 4:41:33 AM11/14/07
to django...@googlegroups.com
[...]

>
> Why?
> Is this a bug?

Everything is behaving normally. You just can't do what you're trying to
do. When the class is constructed (when the __new__ method is called),
Django doesn't know (and has no way of possibly knowing) that the "name"
property is simply a proxy for the "__name" Field object. And Django
does special initialisation processing for Field objects, including
creating a list of fields on the model.

So, when you are trying to use your field in a queryset, you must refer
to it using it's true name ("__name" in your case).

Malcolm


--
Atheism is a non-prophet organization.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages