将TabularInline添加到我的管理界面

时间:2015-12-02 14:05:07

标签: python django django-admin inline tabular

我需要一些帮助。 我需要将TabularInline添加到我的管理界面,因为我必须插入至少包含100个元素的元素周期表。

我有模特:

class ElementsTable(models.Model):
    sign = models.CharField(max_length=3,null=False)
    name = models.CharField(max_length=20,null=False)

class ListMaterials(models.Model):
    materials = models.CharField(max_length=30,null=False)
    ElementsTable = models.ManyToManyField('ElementsTable')

在管理员:

from django.contrib import admin
from italgraniti.models import *

class ElementsInline(admin.TabularInline):
    model = ElementsTable
    extra = 3

class ElementsTableAdmin(admin.ModelAdmin):
    list_display = ('sign', 'name')
    inlines = [ElementsInline]

class AnalisysAdmin(admin.ModelAdmin):
    list_display = ('id','nprova')

class ListMaterialsAdmin(admin.ModelAdmin):
    list_display = ('materials',)

admin.site.register(Analisys, AnalisysAdmin)
admin.site.register(ElementsTable, ElementsTableAdmin)
admin.site.register(ListMaterials, ListMaterialsAdmin)

但它返回:

SystemCheckError: System check identified some issues:

ERRORS:
<class 'italgraniti.admin.ElementsTable'>: (admin.E202) 'italgraniti.ElementsTable' has no ForeignKey to 'italgraniti.ElementsTable'.

我会一次插入近10个元组(符号,名字)..

(django 1.8,python 3.5) 修改:插入整个管理

修改:我很抱歉,但我在英文admin.py和model.py中进行了错误处理。我现在纠正了他们。我理解你的解决方案,但结果

<class 'italgraniti.admin.ElementsTabel'>: (admin.E202) 'italgraniti.ElementsTabel' has no ForeignKey to 'italgraniti.ListMaterials'.

1 个答案:

答案 0 :(得分:0)

您似乎意外地将ElementsTable模型放在引号中。

尝试更改:

ElementsTable = models.ManyToManyField('ElementsTable')

elements_table = models.ManyToManyField(ElementsTable, related_name='elencomaterialiAsElementsTable')


修改

看起来您的主要问题是您正在尝试将ElementsTable内联到自身中,这是不可能的。关键是你应该能够在ElencoMateriali管理页面中看到它。我已经清理了代码并在此处添加了该功能:

from django.contrib import admin
from italgraniti.models import Analisys, ElementsTable, ListMaterials, ElencoMateriali

class ElementsTableInline(admin.TabularInline):
    model = ElementsTable
    extra = 3

class ElencoMaterialiAdmin(admin.ModelAdmin):
    list_display = ('materials',)
    inlines = [ElementsTableInline]

class ElementsTableAdmin(admin.ModelAdmin):
    list_display = ('sign', 'name')

class AnalisiAdmin(admin.ModelAdmin):
    list_display = ('id', 'nprova')

class ListMaterialsAdmin(admin.ModelAdmin):
    list_display = ('materials',)

admin.site.register(Analisys, AnalisysAdmin)
admin.site.register(ElementsTable, ElementsTableAdmin)
admin.site.register(ElencoMateriali, ElencoMaterialiAdmin)
admin.site.register(ListMaterials, ListMaterialsAdmin)