On Mon, 2008-02-11 at 06:13 -0800, shabda wrote:
> I have a model something like,
> 
> class Task(models.Model):
>   name = models.CharField()
>   parent_task = models.ForeignKey('Task')
>   is_complete = models.BooleanField()
> 
> class TaskItem(models.Model)
>   name = models.CharField()
>   task = models.ForeignKey(Task)
>   is_complete = models.BooleanField()
> 
> I have written a lot of code using this model. Now I need to change
> the code so that whenever the is_complete is set, we should have some
> more effects. So I am trying to use
> is_complete = property(get_is_complete, set_is_complete) so that I can
> have side effects when is_complete is set. But after this call as
> is_complete is not a models.Field subclass a number of things break.
> (For example forms.ModelField). Is there some way I can achieve this
> effect.

You can't create a property directly like this, since Python will only
see the last declaration and Django needs to introspect the attribute
type to see whether to treat it as a database field.

Aside from the alternative solution Doug Ballanc suggested, another
possibility is to write your own Field subclass that supplies its own
getter and setter interfaces. Have a look at
django.db.models.subclassing for some inspiration (or have a look at the
django-tagging application on Google code for a more direct example). If
you provide __get__ and __set__ methods, they will be called whenever
the attribute on the model is accessed, as per the normal descrptor
protocol. If you haven't worked in this way before, it might take a bit
of experimenting to get it correct (and I'd really recommend reading
django-tagging for an example); this isn't a heavily used area of code,
so it can take a bit to wrap your brain around it.

Regards,
Malcolm

-- 
If it walks out of your refrigerator, LET IT GO!! 
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