Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django管理员一次编辑多个模型_Python_Django - Fatal编程技术网

Python Django管理员一次编辑多个模型

Python Django管理员一次编辑多个模型,python,django,Python,Django,我正在试图找到一种方法来同时添加/编辑两个模型。i、 e: 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管理员,

我正在试图找到一种方法来同时添加/编辑两个模型。i、 e:

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管理员,您将获得一个
+
符号,您可以添加ForeignKey的值。但是,当您想在编辑桌面时编辑foreignkey时,您不能这样做

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

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

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

在管理员中尝试以下操作:

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


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

看一看

我无法给你一篇简单的总结来告诉你如何做到这一点,但我会参考Django文档。你能添加你的
admin.py吗?很抱歉没说出来。我有需要规格的桌面模型。但我还想制作一个名为服务器的模型,它需要规范。这就是我为规格制作模型的原因。因此,当人们想要添加桌面时,他们可以添加规格。当他们添加服务器时,他们也可以添加规范。您可以像我一样使用服务器,查看文档以了解更多信息此时我得到了以下内容:class ServerInLine(admin.StackedInLine):model=Server extra=1 class SpecsManager(admin.ModelAdmin):inlines=[ServerInLine]这使我能够在添加规范时添加服务器。但实际上,我想在添加新服务器时添加规范。因此,当我添加新服务器或桌面时,我想添加规格。然后,服务器和桌面中的“规格”字段应链接到所填写的规格。谢谢你到目前为止的帮助。请你用这个更新你的问题,并确保其格式正确,很难以这种方式阅读。我不认为你可以,它只能以一种方式工作,因为foreignkey在桌面和服务器上,而不在规格中