Tutorial :How dynamic add custom field to model



Question:

How add custom field dynamicly? I'm trying that, but the field won't insert into database when I sync db:

#It use as register(MyModel)  def register(model, attr="my_attr"):        if model in registry:          raise AlreadyRegistered(              _('The model %s has already been registered.') % model.__name__)        registry.append(model)      setattr(model, attr, MyField())        MyField().contribute_to_class(model, attr)     #MyField.contribute_to_class  def contribute_to_class(self, cls, name):       super(MyField, self).contribute_to_class(cls, name)         setattr(cls, self.name, self)       cls.add_to_class('%s_manager' %  name, MyDescriptor())         signals.post_save.connect(self._save, cls, True)  


Solution:1

You probably cannot do that without hacking into Django's internals. The syncdb command inspects the meta object for each model to get a list of fields to create, which is created on class construction time via the metaclass of the django.db.models.Model base class:

class MyModel(models.Model):      my_filed = models.CharField(...)    # Here, class construction is complete and your class has a _meta member.  # If you want, you can check it out in the interactive shell.  meta = MyModel._meta  

After the class construction is complete, e.g. after the DEDENT following the class statement, the meta object is fixed (not affected by modifying the model class) and you will have to hack the meta (which is of course possible) in order to add dynamic fields. But since you are messing with internal objects here, it could render your app incompatible with future releases of Django.

The remaining question is: Why would you want to do that? Since database tables are usually only created once when deploying your app, models are kind of "static".


Solution:2

I was looking for the same thing and had to settle for raw SQL. Although you can use something like SQLAlchemy.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »