Answer the question
In order to leave comments, you need to log in
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
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?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question