Friday, April 30, 2010

Re: Dynamic Model and ModelAdmin based on tables in db

hello, may be this will be useful:

http://code.djangoproject.com/wiki/DynamicModels

On Apr 22, 5:53 am, Massimiliano della Rovere
<massimiliano.dellarov...@gmail.com> wrote:
> An external process creates tables whose names follow the pattern
> "collector_XYZ"; all the tables will have the very same structure.
>
> For each table I need to create a matching django.db.models.Model and
> django.admin.models.ModelAdmin, dynamically, because over time new
> tables will be created and I do not want to restart apache after the
> program is deployed.
>
> How can I hook this generation process to the login function of the
> admin interface so that each time a user logs in django checks if all
> the tables have a corresponding model? Is there a better mount point?
> Is there a more django-nic way to accomplish this?
>
> from django.db import connection, transaction, connections
>
> cursor = connections['collector'].cursor()
> cursor.execute('SHOW TABLES LIKE "collector_%%"')
> # table name: collector_identifier_nn
> # identitifer is unique among all the tables
> # nn is a two digit number
>
> models = {}
> for t in cursore.fetchall():
> ..values = str(t).split('_')[1:]
> ..models[values[0]] = generaCollector(values[0], values[1])
> ..admin.site.register(models[-1])
>
> def generaCollector(cliente, num):
> ..if type(num) != type(''):
> ....raise TypeError('parameter client must be of type %s insted of %s'
> % (type(''), type(cliente)))
> ..if type(cliente) != type(''):
> ....raise TypeError('parameter num must be of type %s insted of %s' %
> (type(''), type(num)))
> ..cliente = str(cliente).lower()
> ..nome = 'Collector%s%s' % (cliente.capitalize(), num)
> ..ret = type(nome, (Collector,), {'__module__':
> Collector.__dict__['__module__']})
> ..ret._meta.db_table = 'collector_%s_%s' % (cliente, num)
> ..return ret
>
> class Collector(models.Model):
> ..id = models.IntegerField(primary_key=True)
> ..unique_check = models.CharField(unique=True, max_length=240, blank=True)
> ..collector = models.CharField(max_length=96, blank=True)
> ..agent_host = models.CharField(max_length=384, blank=True)
> ..insert_time = models.DateTimeField(null=True, blank=True)
> ..# etc etc
>
> ..def __unicode__(self):
> ....return self.insert_time.isoformat()
>
> ....class Meta:
> ......db_table = u'collector'
> ......abstract = True
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate