Wouldn't it be simpler to have two abstract classes, one, not having the field, and the other being a sub class of the first, and just adding the field?
On Thu, Mar 21, 2013 at 5:05 PM, Alan Johnson <a...@breakrs.com> wrote: > I've got an abstract model in my project that I want to use to define a > field by default on concrete subclasses, but also to allow that field to be > redefined as something other than the default dynamically. All of this > works right now: > > class classproperty(object): > """ > Decorator for making class properties > """ > def __init__(self, fget): > self.fget = fget > > def __get__(self, owner_self, owner_cls): > return self.fget(owner_cls) > > class BaseModel(models.Model): > class Meta(object): > abstract = True > > @classproperty > def _special_attribute_field(self): > return getattr(self, '_bm_special_attribute_field', 'default') > > @property > def bm_special_attribute(self): > return getattr(self, self._special_attribute_field) > > ...and then there are a bunch of methods that use the latter two functions > to figure out which field to access. > > The problem is that right now, classes inheriting from `BaseModel` have to > define the `default` field explicitly, even if they don't use > `_bm_special_attribute_field` to specify something other than the default. > What I'd like to do is programmatically define `default` on concrete > submodels *only if* those models don't use `_bm_special_attribute_field` to > change it to something else, in which case, they should bring their own > field. Is there a way to do this, perhaps with metaclasses or something? > The key thing being that it has to not muck up the Django machinery. > > (cross posted from > http://stackoverflow.com/questions/15249306/django-conditionally-defined-model-field-in-abstract-model, > if you want to answer for points) > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.