I am implementing a calendar in Django and want to expand my functionality and add support for recurring events. My problem is that I would like to have a BooleanField that toggles between one-time / multi-day events and repeating events. Thus, the field will be marked as something like: Is this a recurring event? and if the user checks the checkbox, he changes the start and end fields to display another field.
Here is a short version of my model.py
class Event(models.Model):
title = models.CharField(max_length=40)
description = models.TextField(blank=True)
is_recurring = models.BooleanField(blank=True, default=False, verbose_name='Event is recurring?')
start = models.DateTimeField()
end = models.DateTimeField()
def __str__(self):
return self.title
And if the user checks the is_recurring field, then dynamically delete these 2 fields and change the form. Here is my admin.py:
class EventAdmin(admin.ModelAdmin):
form = EventForm
fieldsets = [
(None, {'fields': [
'title',
'description'
]
}
),
(None, {'fields': [
'is_recurring',
'start',
'end',
]
}
),
]
def get_fieldsets(self, *args, **kwargs):
return(
(None, {
'fields' : ('my_extra_field',),
}),
)
admin.site.register(Event , EventAdmin)
and here are my forms.py
class EventForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['my_extra_field'] = forms.CharField()
class Meta:
model = Event
So, here I tried to add my_extra_fieldto the form to try, but I keep getting
(my_extra_field), . // EventAdmin.
. , , fieldsets, , , , , , , fieldsets.
, , , ? , 2 ?