Glad though I am that Python has dynamic class attributes, when it
comes to Django Models, some more discipline could be useful,
especially when a product is in maintenance phase, IMO.
I recently added the code below to my model definition and it saved me
a bunch of time after some model evolution, by making sure that old
attributes were not still being referred to in templates.
Wondering whether code like this could be implemented as decorator or
activated as an optional meta attribute to the Django Model class if
found generally useful, ideally it should be possible to detail any
exceptions for those rare cases when the usual Python dynamic run-time
attribute get/set behaviour is still required?
It would be good to hear informed comments, in case I am completely
wrong headed about this...
def __setattr__(self, name, value):
if name.startswith("_") or name.endswith("id") or \
name in self.__slots__:
self.__dict__[name] = value
else:
raise AttributeError, "Illegal set attempt: %s = %s" %
(name, value)
def __getattr__(self, name):
if name.startswith("_") or name.endswith("id") or \
name in self.__slots__:
if self.__dict__.has_key(name):
return self.__dict__[name]
else:
return getattr(super(Listing, self), name)
else:
raise AttributeError, "Illegal get attempt: %s" %name
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---