Ticket #8270: contenttype-label.diff

File contenttype-label.diff, 1.0 KB (added by Jeremy Dunck, 16 years ago)

Adds get_for_app_label_and_model_name to ContentTypeManager. Obviously a terrible name.

  • django/contrib/contenttypes/models.py

    old new  
    88    # This cache is shared by all the get_for_* methods.
    99    _cache = {}
    1010   
     11    def get_for_app_label_and_model_name(self, app_label, model_name):
     12        key = (app_label.lower(), model_name.lower())
     13        try:
     14            return self.__class__._cache[key]
     15        except KeyError:
     16            try:
     17                ct = self.get(app_label=app_label,
     18                        model=model_name)
     19            except models.ObjectDoesNotExist:
     20                ct = self.get_for_model(models.get_model(app_label, model_name))
     21        self._add_to_cache(ct)               
     22        return ct
     23
    1124    def get_for_model(self, model):
    1225        """
    1326        Returns the ContentType object for a given model, creating the
Back to Top