I would like to retrieve the Model
object while knowing the table_name
For ex:
class User(models.Model):
class Meta:
db_table = 'my_users_table'
Are there ways that return User
by taking my_users_table
as input?
I would like to retrieve the
Model
object
I think you mean the Model class here instead of object.
One possible solution that I can think of is to get all the models and match for db_table
:
from django.apps import apps
model = next((m for m in apps.get_models() if m._meta.db_table=='my_users_table'), None)
If there is no model with the given db_table name then the model
will be None
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With