Created
March 7, 2018 13:25
-
-
Save nehajagadeesh/7aff03a7dcbdeae130fad698fad3eb5b to your computer and use it in GitHub Desktop.
Custom Contenttype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.apps import apps | |
from django.db import models | |
from django.contrib.contenttypes.models import ContentTypeManager | |
from django.utils.translation import gettext_lazy as _ | |
class CustomContentType(models.Model): | |
app_label = models.CharField(max_length=100) | |
model = models.CharField(_('python model class name'), max_length=100) | |
objects = ContentTypeManager() | |
class Meta: | |
verbose_name = _('content type') | |
verbose_name_plural = _('content types') | |
unique_together = (('app_label', 'model'),) | |
app_label = "goibibo_inbuilt" | |
db_table = "django_content_type" | |
def __str__(self): | |
return self.name | |
@property | |
def name(self): | |
# if issue return self.model | |
model = self.model_class() | |
if not model: | |
return self.model | |
return str(model._meta.verbose_name) | |
def model_class(self): | |
"""Return the model class for this type of content.""" | |
try: | |
app_label = 'goibibo_models' | |
return apps.get_model(app_label, self.model) | |
except LookupError: | |
return None | |
def get_object_for_this_type(self, **kwargs): | |
""" | |
Return an object of this type for the keyword arguments given. | |
Basically, this is a proxy around this object_type's get_object() model | |
method. The ObjectNotExist exception, if thrown, will not be caught, | |
so code that calls this method should catch it. | |
""" | |
return self.model_class()._base_manager.using(self._state.db).get(**kwargs) | |
def get_all_objects_for_this_type(self, **kwargs): | |
""" | |
Return all objects of this type for the keyword arguments given. | |
""" | |
return self.model_class()._base_manager.using(self._state.db).filter(**kwargs) | |
def natural_key(self): | |
app_label = 'goibibo_models' | |
return (app_label, self.model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment