sidz24x7
sidz24x7

Reputation: 15

Loop and Print Verbose_Name of all tables in Django's Model.py

I have a situation where I would like to loop over all the tables available in database(stored in models.py) and print the meta attribute verbose_name only for tables having this attribute in the django html template. .This way the user can select the tables he want to view.

I have tried a few methods, but none seem to work..

This is my code :-

def tables_select(request):
if request.user.is_authenticated():
    mdls = models.get_models(include_auto_created=False)
    tables_list = [mdl._meta.verbose_name for mdl in mdls]
    return render(request, 'cbm/tables_select.html', {'tables_list': tables_list})
else :
...

a check using hasattr() method doesn't work either :

for mdl in mdls:
        if (hasattr(mdl._meta, 'verbose_name')):
            tables_list.append(mdl._meta.verbose_name)

For the tables not having the verbose_name parameter, both of them return actual db table name.

UPDATE: These are the 2 model definitions in models.py :

This is the master table

class Ai(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)  
well = models.ForeignKey('Well', db_column='Well_ID')  
samplename = models.CharField(db_column='sampleName', max_length=40, blank=True)  
startdepth = models.FloatField(db_column='startDepth', blank=True, null=True)  
... # Plus some other Columns

def __unicode__(self):
    return self.samplename

class Meta:
    managed = False
    db_table = 'ai'
    verbose_name = "Adsorbtion Isotherm"

AichIo is a dependent table on Ai

class Aich4Io(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)  
ai = models.ForeignKey(Ai, db_column='AI_ID')  
pressurekpa = models.IntegerField(db_column='pressureKPa', blank=True, null=True)  
pressureatm = models.FloatField(db_column='pressureAtm', blank=True, null=True)  
meccreported = models.FloatField(db_column='meccReported', blank=True, null=True)  
dafccreported = models.FloatField(db_column='dafccReported', blank=True, null=True)  

class Meta:
    managed = False
    db_table = 'aich4io'

I don't want the names of dependent tables to be printed. However for my html template:

  {% for table in tables_list %}
    {{ table }}<br>
  {% endfor %}

The output is:

Adsorbtion Isotherm
aich4 io

Upvotes: 0

Views: 489

Answers (2)

sidz24x7
sidz24x7

Reputation: 15

After much research/combinations was able to find out this is generic behavior of django. For models not having verbose_name parameter, the table/model name is returned as string.

To achieve my objective, I added verbose_name = 'DNS' for the models for which I didn't want to display the verbose_name. And in my code i did something like:

if mdl._meta.verbose_name == 'DNS':
    # Do Nothing
else:
    # My Logic.

Upvotes: 1

hellsgate
hellsgate

Reputation: 6005

You can check what type the verbose name is. If it's unicode then you can print the name

mdls = models.get_models(include_auto_created=False)
for mdl in mdls:
    if isinstance(mdl._meta.verbose_name, unicode):
        print mdl._meta.verbose_name

Upvotes: 0

Related Questions