假设我在保存代码中,如何获取模型的名称或对象的内容类型,并使用它?
from django.db import models
class Foo(models.Model):
...
def save(self):
I am here....I want to obtain the model_name or the content type of the object
这段代码可以执行,但是我必须知道model_name是什么:import django.db.models
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get(model=model_name)
model = content_type.model_class()