Django 在管理中遇到 generic.GenericTabularInline 问题

发布于 2024-11-28 07:51:51 字数 2722 浏览 0 评论 0原文

我有一个通用的 Django 媒体模型,我想将其与许多其他模型相关联。一切似乎都工作正常,但我无法使用管理中的内联表单保存新的或现有的对象。以下是相关模型:

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')

这是管理代码:

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)

这是有问题的表单:

from models import *
from django import forms

class MediaFormInline(forms.ModelForm):

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

内联表单显示正确,但当我保存时出现以下错误:


Django 版本:1.3 pre-alpha 异常类型:TypeError 异常值:

“NoneType”对象不可迭代

异常位置:...django\contrib\contenttypes\generic.py in _set_,第 217 行


发生错误使用 ReverseGenericRelatedObjectsDescriptor 类的 _set_ 方法。该页面正在向 _set_ 的 value 参数发送 None:

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

    manager = self.__get__(instance)
    manager.clear()
    for obj in value:
        manager.add(obj)

任何有关此问题的帮助将不胜感激。如果我不能解决这个问题,我将不得不寻求一个不那么干燥的解决方案。

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__(instance)
    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2024-12-05 07:51:51

您需要使用GenericTabularInline。请参阅 Django 文档< /a>.

样本:

from django.contrib.contenttypes.admin import GenericTabularInline


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


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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文