开发者

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

开发者 https://www.devze.com 2023-01-02 10:08 出处:网络
let\'s assume that I have very basic model class Message(models.Model): msg = models.CharField(max_length=30)

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消