Hello, Pythoners!
It is possible to add properties and methods to classes dynamically,
like this
{{{
def add_method():
def get_username(self):
return self.username
from django.contrib.auth.models import User
User.get_username = get_username
def call_added_method():
from django.contrib.auth.models import User
return User.objects.get(username="demo").get_username()
>>> add_method()
>>> call_added_method()
u'demo'
}}}
Let's say, I need an additional field which rather fits to the User
model than to the profile, i.e. previous_login which saves the date of
login which happened before current login (last_login).
I can add that field to the User model dynamically (for example, using
signals), instead of
1. adding that field to the profile where it makes no sense, or
2. copying and modifying the auth app and then rewriting all admin app
and other User-related parts of Django.
Do you consider this approach for extending existing code as a smart
python programming solution or an ugly hack?
Regards,
Aidas Bendoraitis aka Archatas
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---