django-admin - 如何修改 ModelAdmin 以一次创建多个对象?
假设我有一个非常基本的模型,
class Message(models.Model):
msg = models.CharField(max_length=30)
该模型是在管理模块中注册的:
class MessageAdmin(admin.ModelAdmin):
pass
admin.site.register(Message, MessageAdmin)
目前,当我进入管理界面时,单击“添加消息”后,我只有一种可以输入 msg
的表单。
我想在“添加页面”上有多个表单(可能是表单集),以便我可以一次创建多条消息。每次都必须单击“保存并添加另一个”,这真的很烦人。
理想情况下,我希望实现类似 InlineModelAdmin
的功能,但事实证明,您只能将它用于与编辑的对象相关的模型。
您建议使用什么来解决这个问题?
let's assume that I have very basic model
class Message(models.Model):
msg = models.CharField(max_length=30)
this model is registered with admin module:
class MessageAdmin(admin.ModelAdmin):
pass
admin.site.register(Message, MessageAdmin)
Currently when I go into the admin interface, after clicking "Add message" I have only one form where I can enter the msg
.
I would like to have multiple forms (formset perhaps) on the "Add page" so I can create multiple messages at once. It's really annoying having to click "Save and add another" every single time.
Ideally I would like to achieve something like InlineModelAdmin
but it turns out that you can use it only for the models that are related to the object which is edited.
What would you recommend to use to resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能不完全是您正在寻找的,但是如果您想同时创建多个对象,您可以这样做:
基本上您正在做的是为您的管理模板创建一个自定义表单,它询问用户如何该对象将被创建很多次。然后在 save_model 方法中解释逻辑。
This may not be exactly what you are looking for, but if you want to create multiple objects at the same time you could to somehthing like this:
Basicly what you are doing is creating a custom form for your admin template, which ask the user how many times the object shall be created. The logic is than interpreted in the save_model method.
作为一种解决方法,因为您可能对
User
有 FK,因此您可以在User
模型上定义一个InlineModel
。否则,最简单的方法可能是创建自定义管理视图,因为没有显示和保存表单集的通用管理视图。
As a workaround, Since, It is likely that you have a FK to
User
, so you could define anInlineModel
on theUser
model.Otherwise, the easiest approach may be to create a custom admin view since, there isn't a generic admin view that displays and saves formsets.
如果您使用内联,这很容易。然后您可以使用
extra = 10
或任意数量的额外表单集。似乎没有ModelAdmin
的等效项。当然,在您的消息模型中,您需要为某种消息分组模型创建一个
ForeignKey
作为另一层功能,并获取您正在寻找的多表单集布局。例如:
这将为您提供管理视图中所需的内容并创建分组(即使您只允许一个组),唯一的额外字段是组模型中的
名称
。我什至不确定你是否需要那个。另外,我确信可以为任意值动态生成extra
的值。我希望这有帮助!
This is easy if you are using an Inline. Then you could use
extra = 10
or however many extra formsets you want. There doesn't seem to be an equivalent for theModelAdmin
.Of course in your messages model you would need to create a
ForeignKey
to some sort of message grouping model as another layer of function and to get the multi-formset layout that you are looking for.For example:
This would give you what you want in the Admin view and create grouping (even if you only allow for one group) and the only extra field would be the
name
in the group model. I am not even sure you would need that. Also I am sure the value forextra
could be generated dynamically for an arbitrary value.I hope this helps!