Add a field to the ModelAdmin Form
I want to give users the possibility to create multiple events at once.
Therefore I would like to add a field to the admin-add-page where a number
of repetitions can be specified. Then I want to override the save function
and create multiple events (based on the input). I started writing some
code but the admin add page does not update at all. I will show you the
code below:
In admins.py:
class EventAdmin(admin.ModelAdmin):
    form = EventForm
admin.site.register(Event, EventAdmin)
In forms.py
from django import forms
from django.db import models
from calendar_app.models import Event
class EventForm(forms.ModelForm):
    name = models.CharField(max_length=100) # just for testing purpose
    class Meta:
        model = Event
    def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args, **kwargs)
        if not kwargs.has_key('instance'):
            self.fields['name'] = forms.CharField(label='Name')
            self.base_fields['name'] = forms.CharField(label='Name')
    def save(self, commit=True):
        model = super(EventForm, self).save(commit=False)
        # Save all the fields...
        if commit:
            model.save()
        return model
But the "name" field is not showing up when I add an event. Any ideas?
Thanks!
 
No comments:
Post a Comment