Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tightened up code in django.contrib.contenttypes.models.ContentTypeMa…

…nager.get_for_model() to use get_or_create()

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a4b11826a73e50e3d7d7dfa5d086e409be2b05dc 1 parent fb1c01b
@adrianholovaty adrianholovaty authored
Showing with 5 additions and 10 deletions.
  1. +5 −10 django/contrib/contenttypes/models.py
View
15 django/contrib/contenttypes/models.py
@@ -8,16 +8,11 @@ def get_for_model(self, model):
ContentType if necessary.
"""
opts = model._meta
- try:
- return self.model._default_manager.get(app_label=opts.app_label,
- model=opts.object_name.lower())
- except self.model.DoesNotExist:
- # The str() is needed around opts.verbose_name because it's a
- # django.utils.functional.__proxy__ object.
- ct = self.model(name=str(opts.verbose_name),
- app_label=opts.app_label, model=opts.object_name.lower())
- ct.save()
- return ct
+ # The str() is needed around opts.verbose_name because it's a
+ # django.utils.functional.__proxy__ object.
+ ct, created = self.model._default_manager.get_or_create(app_label=opts.app_label,
+ model=opts.object_name.lower(), defaults={'name': str(opts.verbose_name)})
+ return ct
class ContentType(models.Model):
name = models.CharField(maxlength=100)
Please sign in to comment.
Something went wrong with that request. Please try again.