来自models.py
class Indicator(models.Model):
name = models.CharField(max_length=50)
youtube = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN)
description = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN)
recommendation = models.CharField(max_length=LONG_BLOG_POST_CHAR_LEN)
isPublic = models.BooleanField(default=False)
methods_path = models.CharField(max_length=100,default=None)
meta_description = models.CharField(max_length=150,default='')
image_path = models.CharField(max_length=100,blank=True)
def __str__(self):
return self.name
class IndicatorParameterInt(models.Model):
name = models.CharField(max_length=50)
value = models.IntegerField(default=1)
indicator_int_parameter = models.ForeignKey(Indicator, on_delete=models.CASCADE)
hidden = models.BooleanField(default=False)
class IndicatorParameterFloat(models.Model):
name = models.CharField(max_length=50)
setting = models.FloatField(default=1)
indicator_float_parameter = models.ForeignKey(Indicator, on_delete=models.CASCADE)
hidden = models.BooleanField(default=False)
class Comparison(models.Model):
name = models.CharField(max_length=100)
来自admin.py
from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register( MarketData )
admin.site.register( Indicator )
admin.site.register( UserProfile )
class IndicatorParameterIntInline(admin.TabularInline):
model = IndicatorParameterInt
fk_name = "indicator_int_parameter"
class IndicatorParameterFloatInline(admin.TabularInline):
model = IndicatorParameterFloat
fk_name = "indicator_float_parameter"
class ComparisonInline(admin.TabularInline):
model = Comparison
fk_name = "Comparison"
class IndicatorInline(admin.ModelAdmin):
inlines = [
IndicatorParameterIntInline,
IndicatorParameterFloatInline,
ComparisonInline,
]
admin.site.unregister( Indicator )
admin.site.register( IndicatorInline )
错误 TypeError:' MediaDefiningClass'对象不可迭代出现在admin的最后一行:admin.site.register(IndicatorInline)。如果我首先尝试注册IndicatorInline或任何外键类都没有关系。
我引用了this post,鼓励使用fk_name属性。无论我是否使用fk_name,都会发生错误。
答案 0 :(得分:5)
注册模型管理类时需要指定模型。
admin.site.register(Indicator, IndicatorInline)
请注意,如果您从模块顶部删除admin.site.register( Indicator )
,则无需稍后致电admin.site.unregister(Indicator)
。
我建议重命名IndicatorInline
- 这是一个内联的模型管理员,因此最好将其命名为IndicatorAdmin
。在这种情况下,您可以注册:
admin.site.register(Indicator, IndicatorAdmin)
答案 1 :(得分:3)
首先,尽量不要使用import *
- 始终导入您正在使用的模型。
接下来,我认为改变这一点就足够了
from django.contrib import admin
from .models import *
class IndicatorParameterIntInline(admin.TabularInline):
model = IndicatorParameterInt
fk_name = "indicator_int_parameter"
class IndicatorParameterFloatInline(admin.TabularInline):
model = IndicatorParameterFloat
fk_name = "indicator_float_parameter"
class ComparisonInline(admin.TabularInline):
model = Comparison
fk_name = "Comparison"
class IndicatorInline(admin.ModelAdmin):
inlines = [
IndicatorParameterIntInline,
IndicatorParameterFloatInline,
ComparisonInline,
]
admin.site.register(Indicator, IndicatorInline)
admin.site.register(MarketData, )
admin.site.register(UserProfile, )