简体   繁体   中英

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 .

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?

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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