django 通过模型获取惰性引用
django get lazy reference by Model
要从延迟引用中获取模型,我们可以这样做
from django.apps import apps
Article = apps.get_model('articles.Article')
# app.Model
但是反过来呢?我的意思是通过 Model
class.
获得惰性引用 app.Model
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.
使用meta
选项,
model_meta = Article._meta
lazy_ref = f"{model_meta.object_name}.{model_meta.app_label}"
要从延迟引用中获取模型,我们可以这样做
from django.apps import apps
Article = apps.get_model('articles.Article')
# app.Model
但是反过来呢?我的意思是通过 Model
class.
app.Model
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.
使用meta
选项,
model_meta = Article._meta
lazy_ref = f"{model_meta.object_name}.{model_meta.app_label}"