Tightened up code in django.contrib.contenttypes.models.ContentTypeManager.get_for_model() to use get_or_create()
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
fb1c01b103
commit
a4b11826a7
|
@ -8,16 +8,11 @@ class ContentTypeManager(models.Manager):
|
||||||
ContentType if necessary.
|
ContentType if necessary.
|
||||||
"""
|
"""
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
try:
|
# The str() is needed around opts.verbose_name because it's a
|
||||||
return self.model._default_manager.get(app_label=opts.app_label,
|
# django.utils.functional.__proxy__ object.
|
||||||
model=opts.object_name.lower())
|
ct, created = self.model._default_manager.get_or_create(app_label=opts.app_label,
|
||||||
except self.model.DoesNotExist:
|
model=opts.object_name.lower(), defaults={'name': str(opts.verbose_name)})
|
||||||
# The str() is needed around opts.verbose_name because it's a
|
return ct
|
||||||
# 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
|
|
||||||
|
|
||||||
class ContentType(models.Model):
|
class ContentType(models.Model):
|
||||||
name = models.CharField(maxlength=100)
|
name = models.CharField(maxlength=100)
|
||||||
|
|
Loading…
Reference in New Issue