django-admin2
django-admin2 copied to clipboard
divio: fieldsets should be easy to change
Fieldsets should be easy to add and more important: they should be easy to dynamically change.
@digi604 Can you provide a sample API for changeable fieldsets?
I don't really know where the right place is... if it should be on the form, on the admin class or even in the template. Let me think about it.
Absolutely a +1 from me on this.
I think it should live on the change-form-view since that's the only place were it will be used. A sample API how to change it would be havin a method that gets the default fieldset object and returns the changed one:
class MyChangeFormView(...):
def get_fieldset(self, fieldsets):
# delete a field from the second fieldset
for i, field in list(enumerate(fieldsets[1]['fields'])):
if field == 'field_i_want_to_delete':
del fieldset[1]['fields'][i]
break
# add a new field at the end of the first fieldset
fieldsets[0]['fields'].append('my_extra_field')
# add a class to the last fieldset
fieldsets[-1]['classes'].append('wide')
return fieldsets
The example is using the fieldsets definition that django.contrib.admin
is using (read: dicts and lists). We could also introduce a Fieldset
object that has somehelper methods, so it makes changing it easier, like:
class MyChangeFormView(...):
def get_fieldset(self, fieldsets):
# delete a field from the second fieldset
fieldsets.remove_field('field_i_want_to_delete')
# add a new field at the end of the first fieldset
fieldsets[0].add_field('my_extra_field')
# add a class to the last fieldset
fieldsets[-1].add_css_class('wide')
return fieldsets
I like your approach, since it removes the scoping issue from the equation. The only downside I can think of is documenting how this is done. :-)
I started some work here: https://github.com/gregmuellegger/django-admin2/compare/fieldsets I hope to make further progress next week.
@gregmuellegger ping
@auvipy Sorry I lost interest in the project a while back, I won't continue work on this in the foreseeable future. Feel free to take the code mentioned in the link above if it is still relevant.
it's OK. I pulled the code. thanks for your earlier efforts.