Django:管理员内联形成每个实例的初始数据 [英] Django: Admin inline forms initial data for every instance
问题描述
我一直在阅读很多,但我似乎无法找出解决方案。
我正在Django中编写一个应用程序我还在写管理员端。
我有一个名为环境的模型和一个称为服务器的模型,服务器和环境之间有一个ForeignKey关系例如给定的环境有几个服务器。
在管理界面中修改环境的添加表单时,我使用内联表单来显示将与环境关联的服务器,如下所示:
class ServerInline(admin.TabularInline):
model = Server
extra = 39
class EnvironmentAdmin(admin.ModelAdmin):
inlines = [ServerInline]
很简单吗?
我想做的是使用默认值预处理服务器内联表单,已经能够预先填写相同的值做到这一点:
class ServerInlineAdminForm(forms.ModelForm):
class Meta:
model = server
def __init __(self,* args,** kwargs):
super(ServerInlineAdminForm,self).__ init __(* args,** kwargs)
self.initial ['name'] ='测试'
class ServerInline(admin.TabularInline):
form = ServerInlineAdminForm
model = Server
extra = 39
class EnvironmentAdmin(admin.ModelAdmin):
inlines = [ServerInline]
但这不是我想要的,我想能够初始化39个服务器窗体实例与39个不同的值,我在一个列表中。那么最好的方式是什么?
谢谢!
我意识到我自己解决了这个问题,在这里没有回答。
我最后所做的是重写环境类save_model方法,而不是使用管理表单。
我会解释一点点:
我有一个环境对象和一个服务器对象。环境中有许多服务器通过外键链接到服务器对象。我的目标是在环境创建过程中填充与环境相关联的服务器。为了能够做到这一点,我重写了Environment对象的save_model方法,做一个obj.save()和AFTERWARDS创建指向这个环境的Server对象,然后再创建一个obj.save()。为什么以后呢因为我不能将新创建的服务器与尚不存在的环境相关联。如果有人对他的实际代码感兴趣,请告诉我。
I've been reading a lot but I don't seem to be able to figure out a solution to this.
I'm writing an application in Django, I'm still writing the admin side.
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]
Pretty simple right?
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]
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??
Thank you!
I realized that I solved the problem myself and hadn't answered here.
What I finally did is to override the Environment class save_model method instead for using the admin forms.
I'll explain a little bit better:
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屋!