简体   繁体   English

django-admin-如何修改ModelAdmin一次创建多个对象?

[英]django-admin - how to modify ModelAdmin to create multiple objects at once?

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 . 目前,当我进入管理界面时,单击“添加消息”后,只有一种表单可以输入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. 理想情况下,我希望实现类似InlineModelAdmin但事实证明,您只能将其用于与已编辑对象相关的模型。

What would you recommend to use to resolve this problem? 您将建议使用什么来解决此问题?

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: 这可能不完全是您想要的,但是如果您想同时创建多个对象,则可以执行以下操作:

#In /forms.py
MessageAdminForm(forms.ModelForm):
    msg = CharField(max_length=30)
    count = IntegerField()

#In /admin.py
from app.admin import MessageAdminForm
MessageAdmin(admin.ModelAdmin):
    form = MessageAdminForm
    fieldsets = (
        (None, {
            'fields' : ('msg','count')    
         }),)
    def save_model(self, request, obj, form, change):
        obj.msg = form.cleaned_data['msg']
        obj.save()
        for messages in range(form.cleaned_data['count']):
            message = Message(msg=form.cleaned_data['msg'])
            message.save()

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. 然后在save_model方法中解释逻辑。

As a workaround, Since, It is likely that you have a FK to User , so you could define an InlineModel on the User model. 作为一种解决方法,因为, User可能有FK,所以可以在User模型上定义一个InlineModel

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. 否则,最简单的方法可能是创建自定义管理员视图,因为没有通用的管理员视图可以显示和保存表单集。

This is easy if you are using an Inline. 如果您使用内联,这很容易。 Then you could use extra = 10 or however many extra formsets you want. 然后,您可以使用extra = 10或您想要的许多额外的模板集。 There doesn't seem to be an equivalent for the ModelAdmin . 似乎没有与ModelAdmin等效的ModelAdmin

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. 当然,在您的消息模型中,您需要为某种消息分组模型创建一个ForeignKey作为另一层功能,并获取您正在寻找的多表单集布局。

For example: 例如:

models.py:
    class Group(models.Model):
        name = models.CharField(max_length=30)
    class Message(models.Model):
        msg = models.CharField(max_length=30)
        grp = models.ForeignKey(Group)

admin.py:
    class MessageInline(admin.TabularInline):
        model = Message
        extra = 10
    class GroupAdmin(admin.ModelAdmin):
        inlines = [MessageInline]
    admin.site.register(Group, GroupAdmin)

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. 这将为您提供在Admin视图中想要的内容并创建分组(即使您只允许一个组),唯一的额外字段将是组模型中的name I am not even sure you would need that. 我什至不确定您是否会需要它。 Also I am sure the value for extra could be generated dynamically for an arbitrary value. 此外,我确信可以为任意值动态生成extra的值。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM