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

发布于 2024-09-04 10:26:23 字数 500 浏览 4 评论 0原文

假设我有一个非常基本的模型,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网白 2024-09-11 10:26:23

这可能不完全是您正在寻找的,但是如果您想同时创建多个对象,您可以这样做:

#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()

基本上您正在做的是为您的管理模板创建一个自定义表单,它询问用户如何该对象将被创建很多次。然后在 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:

#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.

呆萌少年 2024-09-11 10:26:23

作为一种解决方法,因为您可能对 User 有 FK,因此您可以在 User 模型上定义一个 InlineModel

否则,最简单的方法可能是创建自定义管理视图,因为没有显示和保存表单集的通用管理视图。

As a workaround, Since, It is likely that you have a FK to User, so you could define an InlineModel on the User 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.

夏末 2024-09-11 10:26:23

如果您使用内联,这很容易。然后您可以使用 extra = 10 或任意数量的额外表单集。似乎没有 ModelAdmin 的等效项。

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

例如:

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)

这将为您提供管理视图中所需的内容并创建分组(即使您只允许一个组),唯一的额外字段是组模型中的名称。我什至不确定你是否需要那个。另外,我确信可以为任意值动态生成 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 the 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.

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. 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.

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文