On 03/03/2011 04:33 PM, bruno desthuilliers wrote:
It has nothing to do with "looking cool" or anything like that. Using
models.fields as class attributes in models class statement's body
allow for the ORM to know what db fields and relations your table has
- not what instance attributes a model instance will have. Well, not
directly at least - of course the Model base case makes sure your
instance will have corresponding instance attributes (for db fields),
but note that these instance attributes are just plain python
attributes, NOT the models.fields you defined at the class level.
I have a question about this point.
In one Django application I'm writing, I would like to programatically
add some attributes to a model instance when it's retrieved from the
database.
I thought this would work:
class Foo(models.Model):
bar = models.CharField(max_length=10)
def __init__(self, *args, **kwargs):
super(Foo, self).__init__(self, *args, **kwargs)
self.foobar = do_something(bar)
f = Foo(bar='test')
Until then, it's ok: the model instance 'f' is created and so the
attribute f.foobar; but when I try to save the instance to the db
f.save()
Django fails with an obscure (for me) error:
------------------------------------------------------
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/base.py",
line 458, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/base.py",
line 520, in save_base
manager.using(using).filter(pk=pk_val).exists())):
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/query.py",
line 561, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/query.py",
line 579, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/sql/query.py",
line 1170, in add_q
can_reuse=used_aliases, force_having=force_having)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/sql/query.py",
line 1105, in add_filter
connector)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/sql/where.py",
line 67, in add
value = obj.prepare(lookup_type, value)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/sql/where.py",
line 316, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/fields/__init__.py",
line 292, in get_prep_lookup
return self.get_prep_value(value)
File
"/home/lorenzo/.virtualenvs/virgo/lib/python2.6/site-packages/django/db/models/fields/__init__.py",
line 479, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'Foo'
-----------------------------------------------
What's wrong with this approach ?
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.