python - Django Admin 一次编辑多个模型

标签 python django

我正在尝试找到一种同时添加/编辑两个模型的方法。即:

class Desktop(models.Model):
    #some field...

    specs = models.ForeignKey(Specs)

class Specs(models.Model):
    cpu = models.CharField(max_length=200)
    #and some other fields

当我添加新桌面时,我希望能够同时添加规范。 使用普通的 Django Admin,您将获得一个 + 符号,您可以添加 ForeignKey 的值。但是当你想在编辑桌面的同时编辑外键时,你就做不到了。

更新! 我添加了以下内容:

class ServerInLine(admin.StackedInLine): 
    model = Server 
    extra = 1  
class SpecsManager(admin.ModelAdmin): 
    inlines = [ServerInLine]

这使我能够在添加规范时添加服务器。但实际上我想在添加新服务器时添加规范。因此,当我添加新的服务器或桌面时,我想添加规范。 Server 和 Desktop 中的规范字段应链接到填写的规范。

最佳答案

在你的管理员中试试这个:

 class DesktopInline(admin.StackedInline):
    model = Desktop
    extra = 1


class SpecsAdmin(admin.ModelAdmin):
    inlines = [DesktopInline,]
admin.site.register(Specs, SpecsAdmin)

看看the docs

关于python - Django Admin 一次编辑多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516856/

相关文章:

python - 将我自己的文件命名为与模块相同的名称后无法导入 Python 模块

python - 我应该如何在scrapy中选择input_processor和output_processor,我没有看到它们之间有任何不同,因为它们最终将进入管道

python - NoReverseMatch 位于/tinku/update

python - bool 掩码,如果 df 的时间戳与第二个 df 的两个时间点 - python

python - 在奇数和偶数隔离列表中对奇数和偶数进行排序/分组

python - DRF 上传多个文件

python - 如何处理 Python Social Auth 上的异常

css - block 内容中的文本溢出

django - Django 的 QuerySets 是否足够懒惰来处理大数据集?

python 从 os.system stdout 中提取字段