Home > Back-end >  django get lazy reference by Model
django get lazy reference by Model

Time:12-05

To get model from lazy reference, we can do

from django.apps import apps
Article = apps.get_model('articles.Article') 
# app.Model

But what about the reverse for it? I mean to get the lazy reference app.Model by Model class.

from articles.models import Article
# something like
print(Article.lazy_reference)
# <str> articles.Article

you probably wonder why I do this, I need to tract user initiative and passive actions, there's no modelfield in django, hence I store the model reference and pk as indication.

CodePudding user response:

Use the meta options,

model_meta = Article._meta

lazy_ref = f"{model_meta.object_name}.{model_meta.app_label}"
  • Related