How can I obtain the model's name or the content type of a Django object?

后端 未结 2 690
遇见更好的自我
遇见更好的自我 2021-02-11 12:41

Let\'s say I am in the save code. How can I obtain the model\'s name or the content type of the object, and use it?

from django.db import models

class Foo(model         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-11 13:26

    You can get the model name from the object like this:

    self.__class__.__name__
    

    If you prefer the content type, you should be able to get that like this:

    from django.contrib.contenttypes.models import ContentType
    ContentType.objects.get_for_model(self)
    

提交回复
热议问题