python - 管理员中缺少 Django 模型类

标签 python django django-models django-admin

由于某些原因,archivealbum 类在 django 管理面板中显示了它们的所有字段,但是 image 没有显示album 字段,当我将图像添加到图像面板时。如果我打开一个 shell,它显示 albumimage 的一个子集,它只是没有出现在 image 的管理中接口(interface)(但它是通过 CLI 实现的)。为什么?

class archive(models.Model):
    name = models.CharField(max_length = 30)
    archivedata = models.TextField(blank=True, help_text="Documentation for album/image.archivedata methodology is put here")
    def __unicode__(self):
        return self.name

class tag(models.Model):
    archive = models.ForeignKey(archive)
    tag = models.CharField(max_length=60)
    def __unicode__(self):
        return self.tag

class album(models.Model):
    archive = models.ForeignKey(archive)
    title = models.CharField(max_length=60)
    tags = models.ManyToManyField(tag, blank=True, help_text="Searchable Keywords")
    archivedata = models.TextField(blank=True, null=True, help_text="Data specific to particular archiving methods or processes can be stored here")
    def __unicode__(self):
        return self.title

class image(models.Model):
    album = models.ForeignKey(album)
    archive = models.ForeignKey(archive)
    imagefile = models.ImageField(upload_to='ns/') #image.width/height
    title = models.CharField(max_length=60, blank=True, help_text="Descriptive image title")
    tags = models.ManyToManyField(tag, blank=True, help_text="Searchable Keywords")

更新:根据请求包含我的 admin.py:

from django.db.models import get_models, get_app
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered

def autoregister(*app_list):
    for app_name in app_list:
        app_models = get_app(app_name)
        for model in get_models(app_models):
            try:
                admin.site.register(model)
            except AlreadyRegistered:
                pass

autoregister('appname')

最佳答案

您的 admin.py 文件通常应如下所示。

from django.contrib import admin

admin.site.register(archive)
admin.site.register(album)
admin.site.register(image)

根据您的 admin.py,我会这样做。

autoregister('archive', 'album', 'image')

说了几点 - 您的 admin.py 有点过于复杂,当 3 行就足够时不需要。此外,您应该以大写形式命名您的模型(存档、相册、图像)

关于python - 管理员中缺少 Django 模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17714144/

相关文章:

django - 删除陈旧的ContentTypes的最干净方法?

python - 我应该如何处理 @jwt_required 装饰器中引发的异常? (在 flask-jwt-extended 中)

Python多维数组

python - 将 Python 变量从一个函数传递到另一个函数,以将其与 SQL 数据库的变量进行比较

python - 如何在Python中正确导入?

android - 在 django 服务器中获取 Android POST 的数据

python - 过滤 Django 查询集的字典值

python - Django 中 CBV 的设计

python - django - 有什么方法可以替代抽象类的 ForeignKey?

Django 不会在 Postgres 上创建 ImageField