How to get table name from Django filter objects

I have tables with same columns Like

class teachers(models.Model):
    x= models..CharField(max_length=250, blank=True, null=True);
    y= models..CharField(max_length=250, blank=True, null=True);

class students(models.Model):
    x= models..CharField(max_length=250, blank=True, null=True);
    z= models..CharField(max_length=250, blank=True, null=True);

      

I am using a function to process the x column of both tables. So, if any unwanted values ​​go into the value for x, I need to write this with the column name.

Like f = students.objects.filter ()

def validate_x(obj):
    if obj.x == None:
        logger.error("None object found in table" + str(obj__tablename))
        return False
    else:
        return True

for i in f:
    validate_result = validate_x(i)

      

My actual scenario is not null validation. I just tried to explain it with this example. Is there any way to achieve this. I am using Django 1.6

+3


source to share


2 answers


object.__class__.__name__

or object._meta.object_name

must specify the name of the model. (if you require the model name).



when you need db table name then you should use object._meta.db_table

as arpit-solanki said.

+2


source


Use this to get the name of the database table

obj._meta.db_table



This might be useful, but in 1.11

+2


source







All Articles