Django 在管理中遇到 generic.GenericTabularInline 问题
我有一个通用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
GenericTabularInline
。请参阅 Django 文档< /a>.样本:
You need to use
GenericTabularInline
. See the Django docs.Sample: