Snippet List
This snipped removes a specific fields from the fieldsets. This is very useful to leave a field 'out' in the admin, likewise:
def get_fieldsets(self, request, obj=None):
fieldsets = super(BlaModelAdmin, self).get_fieldsets(request, obj)
if not request.user.has_perm('change_blah'):
remove_from_fieldsets(fieldsets, ('blah',))
- admin
- fieldset
- form
- field
- fieldsets
I've devised a DRY method of declaring django fieldsets:
** Example usage: **
1. Include the attached code in `fieldsets.py`
2. `models.py`:
from django.db import models
from fieldsets import Fieldset, ModelWithFieldsets
class Person(ModelWithFieldsets): #instead of models.Model
# this field will be placed in nameless fieldset
example_field = models.IntegerField()
# this fieldset will be grouped into one row
Fieldset(grouped=True)
first_name = models.CharField(max_length=64)
surname = models.CharField(max_length=64)
Fieldset("Contact Details", classes=('collapse',))
mobile_phone = models.CharField(max_length=10)
email_address = models.EmailField()
Fieldset("Address")
street_address = models.CharField(max_length=255)
# the next two fields will be grouped into one row of this fieldset
Fieldset.new_group(2)
suburb = models.CharField(max_length=64)
state = models.CharField(max_length=64)
3. `admin.py`:
from django.contrib import admin
from models import Person
from fieldsets import Fieldset
class PersonAdmin(admin.ModelAdmin):
fieldsets = Fieldset.get_fieldsets(Person)
admin.site.register(Person, PersonAdmin)
This example produces the equivalent of manually typing:
fieldsets = (
(None, {'fields': ('example_field')}),
(None, {'fields': (('first_name', 'surname'),)}),
('Contact Details', {
'fields': ('mobile_phone', 'email_address'),
'classes': ('collapse',)}),
('Address', {'fields': ('street_address', ('suburb', 'state'))})
)
But now if you want to rearrange your fields, rename, delete, insert, etc, you won't need to remember to update the fieldsets in the ModelAdmin.
This implementation is a bit of a hack, but I believe a cleaner equivalent should be implemented in django itself.
This template tag build a Form splitted in fieldsets. The fieldsets are configured with a second parameter, that is a tuple like the one used in the Admin class in models in the attribute "fields".
You pass to the template the form and the tuple and than use them as parameters for the templatetag.
You can take a look at the source and modify It to build forms the way you like.
It is very useful If you do not like the way Django build forms with the methods as_p, as_ul or as_table and also do not like to write html by hand.
- forms
- fieldset
- form
- fieldsets
6 snippets posted so far.