Skip to content
Advertisement

How to get appname from model in django

I have this code

class Option(models.Model):
  option_text  = models.CharField(max_length=400)
  option_num   = models.IntegerField()
  # add field to hold image or a image url in future

  class Meta:
        app_label = 'myapp'

if i have Options model in my view i want to get the appname from it

Advertisement

Answer

from django.contrib.contenttypes.models import ContentType

ct = ContentType.objects.get_for_model(option_instance)
print(ct.app_label)

or

option_instance._meta.app_label
Advertisement