Django:管理内联表单每个实例的初始数据 [英] Django: Admin inline forms initial data for every instance

查看:14
本文介绍了Django:管理内联表单每个实例的初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多,但我似乎无法找到解决方案.

I've been reading a lot but I don't seem to be able to figure out a solution to this.

我正在用 Django 编写应用程序,我还在编写管理端.

I'm writing an application in Django, I'm still writing the admin side.

我有一个名为Environments"的模型和一个名为Servers"的模型,Servers 和 Environments 之间存在 ForeignKey 关系,例如给定 Environment 有多个服务器.

I have a model called "Environments" and a model called "Servers", there is a ForeignKey relation between Servers and Environments such as a given Environment has several servers.

在管理界面中修改环境的添加"表单时,我使用内联表单来可视化将与环境关联的服务器列表,如下所示:

When modifying the "add" form for Environments in the admin interface I use a Inline form to be able to visualize the list of Servers that will be associated to the Environment, something like this:

class ServerInline(admin.TabularInline):
    model = Server
    extra = 39

class EnvironmentAdmin(admin.ModelAdmin):
    inlines = [ServerInline]

很简单吧?

我想做的是使用默认值预填充服务器内联表单,我已经能够使用相同的值预填充它们:

What I would like to do is prepopulate the Servers inline forms with default values, I've been able to prepopulate them with the same value doing this:

class ServerInlineAdminForm(forms.ModelForm):
    class Meta:
        model = Server

    def __init__(self, *args, **kwargs):
        super(ServerInlineAdminForm, self).__init__(*args, **kwargs)
        self.initial['name']='Testing'

class ServerInline(admin.TabularInline):
    form = ServerInlineAdminForm
    model = Server
    extra = 39

class EnvironmentAdmin(admin.ModelAdmin):
    inlines = [ServerInline]

但这不是我想要的,我希望能够用列表中的 39 个不同值初始化 39 个服务器表单实例.最好的方法是什么??

But this isn't what I want, I would like to be able to initialize the 39 Server form instances with 39 different values that I have in a list. What would be the best way to do that??

谢谢!

推荐答案

我意识到我自己解决了问题,这里没有回答.

I realized that I solved the problem myself and hadn't answered here.

我最终所做的是重写 Environment 类 save_model 方法,而不是使用管理表单.

What I finally did is to override the Environment class save_model method instead for using the admin forms.

我会稍微解释一下:

我有一个环境对象和一个服务器对象.一个环境有许多服务器,它们通过服务器对象的外键链接到它.我的目标是在环境创建过程中填充与环境关联的服务器.为了能够做到这一点,我所做的是覆盖 Environment 对象的 save_model 方法,执行 obj.save() 和 AFTERWARDS 创建指向该环境的 Server 对象,然后再次 obj.save().为什么后来?因为我无法将新创建的服务器与尚不存在的环境相关联.如果有人对他的实际代码感兴趣,请告诉我.

I have an environment object and a server object. An environment has a number of servers that are linked to it via a foreign key into the server object. My goal was to populate the servers associated to an environment in the environment creation process. To be able to do that what I did was override the save_model method for the Environment object, do an obj.save() and AFTERWARDS create the Server objects that point to this environment, and then obj.save() again. Why afterwards? Because I can't relation a new created server with an environment that doesn't exist yet. Let me know if there is someone interested on he actual code.

这篇关于Django:管理内联表单每个实例的初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆