Django InlineModelAdmin gives error 'MediaDefiningClass' object is not iterable

情到浓时终转凉″ 提交于 2020-01-14 01:58:48

问题


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

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

The error TypeError: 'MediaDefiningClass' object is not iterable comes up on the final line of admin: admin.site.register( IndicatorInline ). It doesn't matter if I try to register IndicatorInline first or any of the foreign key classes.

I referenced this post, which encouraged using the fk_name attribute. The error occurs regardless of whether or not I use fk_name.


回答1:


First of all, try not to use import * - always import the model that you are using.

Next, I think changing this should be enough

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



回答2:


You need to specify the model when you register the model admin class.

admin.site.register(Indicator, IndicatorInline)

Note that if you remove admin.site.register( Indicator ) from the top of the module, then you won't have to call admin.site.unregister(Indicator) later.

I would suggest renaming IndicatorInline - it's a model admin that has inlines, so it would be better to name it IndicatorAdmin. In that case, you would register it with:

admin.site.register(Indicator, IndicatorAdmin)


来源:https://stackoverflow.com/questions/41878496/django-inlinemodeladmin-gives-error-mediadefiningclass-object-is-not-iterable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!