开发者

Django trouble with generic.GenericTabularInline in admin

I have a generic Django media model that I want to relate to a number of other models. Everything seems to be working fine but I am unable to save new or existing objects using an inline form in the admin. Here are the relevant models:

from django.db import models
from franklin.utils.content_media import *
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from franklin.core.scripts.models import Script

class Media(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    media_type = models.CharField(max_length = 20, choices = media_types)
    media_file = models.CharField(max_length = 300, blank = True)
    text_value = models.CharField(max_length = 2000, blank = True)
    caption = models.CharField(max_length = 300, blank = True)
    attributes = models.CharField(max_length = 150, blank = True)
    related_id = models.PositiveIntegerField(null = True, blank = True)
    template = models.ForeignKey(Script, null = True, blank = True)
    sort_order = models.IntegerField(default = 1)

class StaticContent(models.Model):
    title = models.CharField(max_length = 100, unique = True)
    key = models.CharField(max_length = 200, unique = True)
    content_type = models.CharField(max_length = 10, choices = content_types)
    content = models.TextField(blank = True)
    media = generic.GenericRelation(Media, content_type_field='content_type', object_id_field='object_id')

Here is the admin code:

from django.contrib import admin
from forms import *
from models import *
from django.contrib.contenttypes import generic

class MediaInline(generic.GenericTabularInline):
    model = Media
    form = MediaFormInline
    extra = 1
    verbose_name_plural = 'media'

class static_content_admin(admin.ModelAdmin):
    inlines = [MediaInline]

admin.site.register(StaticContent, static_content_admin)

Here is the offending form:

from models import *
from django import forms

class MediaFormInline(forms.ModelForm):

    class Meta:
        model = Media
        fields = ('media_type', 'sort_order',)

The inline form is displayed properly but when I save I get the following error:


Django Version: 1.3 pre-alpha Exception Type: TypeError Exception Value:

'NoneType' object is not iterable

Exception Location: ...django\contrib\contenttypes\generic.py in _set_, line 217


The error is occurring with the _set_ method of a ReverseGenericRelatedObjectsDescriptor class. The page is sending None to the value parameter of _set_:

def __set__(self, instance, value):
    if instance is None:
        raise AttributeError("Manager must be accessed via instance")

    manager = self.__get__(inst开发者_如何学运维ance)
    manager.clear()
    for obj in value:
        manager.add(obj)

Any help with this will be much appreciated. If I can't solve this, I will have to go to a solution that isn't so DRY.


You need to use GenericTabularInline. See the Django docs.

Sample:

from django.contrib.contenttypes.admin import GenericTabularInline


class MediatInline(GenericTabularInline):
    model = Media
    extra = 1


class StaticContentAdmin(admin.ModelAdmin):
    inlines = [MediaInline]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜