python - Missing metaclass in Django 1.5 -


how override model's metaclass in django 1.5? overriding metaclass on models inheriting abstract model set appropriate choices. e.g.

class basemodel(models.model):      field_with_choices = models.charfield(max_length=100)      class meta:         abstract = true  class mymodelmetaclass(basemodel.__metaclass__):     def __new__(cls, *args, **kwargs):         new_class = super(mymodelmetaclass, cls).__new__(cls, *args, **kwargs)         field = new_class._meta.get_field('field_with_choices')         choices = field._choices = []         choices.extend(get_choices())         return new_class  class mymodel(basemodel):      __metaclass__ = mymodelmetaclass 

however, when upgraded django 1.5, error:

attributeerror: type object 'basemodel' has no attribute '__metaclass__' 

how override model's metaclass in 1.5, or otherwise dynamically set field attributes in model subclasses?

you can use built-in function type this:

class mymodelmetaclass(type(basemodel)):     def __new__(cls, *args, **kwargs):         new_class = super(mymodelmetaclass, cls).__new__(cls, *args, **kwargs)         field = new_class._meta.get_field('field_with_choices')         choices = field._choices = []         choices.extend(get_choices())         return new_class 

although see type(modelbase) type inherit type or maybe model.__metaclass__ in turn modelbase modelbase top of model metaclass architecture (before type of course :d ).

hope helps!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -