On Sun, 2008-02-17 at 18:39 +0100, Florian Lindner wrote:
> Hello,
> I need to modify the date of one model field (TextField) each time  
> before it is displayed. The modifications depend on one other field in  
> the same Model.
> I think the best way to do this is to subclass TextField and override  
> the method to get contents. My questions:
> 
> 1) How is this method called?

There is (normally) no method that is called. When the model is created,
the values are put in as normal Python attributes on the class instance.
It's only when save() is called that we pass those attributes to the
right database fields. If you have a look at type(my_model.my_field) for
a TextField ,you'll see it's just a unicode object.

However, you can set up a __get__ and __set__ method on the attribute if
you need to. That's how field subclassing works, for example. You might
want to have a read of the field subclassing documentation (in the docs/
directory of a recent source checkout).

> 2) How can I access the Model from the Field?

You can't. Normally you don't need to, so we don't both setting up a
weakref for that. A non-weakref would create a circular reference, since
the model has an Options instance attached to it (the _meta attribute),
which contains a list of the fields.

> 3) Is this the way to go?

Yes, but it will require careful implementation. We haven't really
implemented a generic way to have one database field correspond to N
model attributes, which sound like what you're after. It's on the
long-term planning list, but is low priority at the moment.

Still, it should be possible using the descriptor protocol and careful
work.

Malcolm

-- 
What if there were no hypothetical questions? 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to