From 8c48dda3b4c5cf0ac6c2d404ae29d8ef315b4f7f Mon Sep 17 00:00:00 2001 From: melinath Date: Thu, 17 Jun 2010 12:39:12 -0400 Subject: [PATCH] Fixed null content reference id bug. --- admin.py | 17 ++++++++++++----- models.py | 7 +++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/admin.py b/admin.py index 64893d5..ce79635 100644 --- a/admin.py +++ b/admin.py @@ -228,7 +228,7 @@ class PageAdmin(NodeAdmin): initial_content = None try: initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk - except ContentReference.DoesNotExist: + except (ContentReference.DoesNotExist, AttributeError): pass form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', widget=ModelLookupWidget(container_content_type), initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all()) return form @@ -249,10 +249,17 @@ class PageAdmin(NodeAdmin): for container_name, container_content_type in contentreference_containers: if ('contentreference_container_%s' % container_name) in form.cleaned_data: content = form.cleaned_data[('contentreference_container_%s' % container_name)] - contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content}) - if not created: - contentreference.content = content - contentreference.save() + try: + contentreference = page.contentreferences.get(name=container_name) + except: + contentreference = ContentReference(name=container_name, page=page, content_type=container_content_type) + + if content == None: + contentreference.content_id = None + else: + contentreference.content_id = content.id + + contentreference.save() class TemplateAdmin(admin.ModelAdmin): diff --git a/models.py b/models.py index 39f166c..f866ee6 100644 --- a/models.py +++ b/models.py @@ -213,7 +213,10 @@ class InheritableTreeEntity(TreeEntity): @property def instance(self): - return self.instance_type.get_object_for_this_type(id=self.id) + try: + return self.instance_type.get_object_for_this_type(id=self.id) + except: + return None def get_path(self, pathsep='/', field='slug'): path = getattr(self.instance, field, '?') @@ -399,7 +402,7 @@ class ContentReference(models.Model): page = models.ForeignKey(Page, related_name='contentreferences') name = models.CharField(max_length=255) content_type = models.ForeignKey(ContentType, verbose_name='Content type') - content_id = models.PositiveIntegerField(verbose_name='Content ID') + content_id = models.PositiveIntegerField(verbose_name='Content ID', blank=True, null=True) content = generic.GenericForeignKey('content_type', 'content_id') def __unicode__(self): -- 2.20.1