C
C
Caeterra2019-05-08 05:49:39
Django
Caeterra, 2019-05-08 05:49:39

How to replace model name with variable in Django?

Good afternoon. I'm new to Django and I'm having trouble. Tell me how to correctly replace the name of the model with a variable or in which direction to dig? The database uses many identical tables (ca. 2000) with the same structure. I need to replace table_name with key without creating 2000 models.
MODEL:
class table_name(models.Model):
ttl = models.CharField(max_length=50)
ttl_lnk = models.CharField(max_length=50)
VIEWS:
def lnk(request, key):
result = key.objects.all()
return render(request, 'index/index.html', {'key': result})
Tried with getattr: result= getattr(table_name, 'key').objects.all()
But I'm doing something wrong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-05-08
@Caeterra

if the structure is the same, then why not combine all the data into one table and add the TypeRec field, which will store the desired type value?

A
Alexander, 2019-05-08
@NeiroNx

Obviously you need a global variable:
global VARIABLE
VARIABLE = MODEL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question