#12781: model object should offer a way to get the table name ------------------------------------------+--------------------------------- Reporter: sienkiew | Owner: nobody Status: new | Milestone: Component: Database layer (models, ORM) | Version: 1.1 Keywords: enhancment request | Stage: Unreviewed Has_patch: 1 | ------------------------------------------+--------------------------------- If you want to make a direct SQL query, you need to know the table name, but the table name can be changed in various ways by the models.py file and the django database layer.
You can find the name of the table by {{{ name = model_object._meta.db_table }}} but that _ in the name indicates a private field that is not intended for the application to use. It would be nice to have an approved way to get the table name, so I could say, for example: {{{ cursor.execute("SELECT DISTINCT colname FROM %s" % model_object.get_table_name() ) }}} I would not prefer to just write the table name in the SQL because my models.py file overrides the table names so that we can have multiple independent developers share the same debug database server. That is, we use something like: {{{ class model_object( models.Model ) : class Meta : db_table = local_config.developer_name + '_model_object' }}} to ensure that the tables have distinct names. The obvious workaround is: {{{ class model_object( models.Model ) : @classmethod def get_table_name( cls ) : return cls._meta.db_table }}} I think this would work if you put this workaround in class ModelBase or class Model in django/db/models/base.py . I'm not entirely sure why there is a distinction between ModelBase and Model, so I can't present an actual patch, but I think this is close enough to check the "has patch" box below. It is, of course, subject to some architectural review too. -- Ticket URL: <http://code.djangoproject.com/ticket/12781> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.