Login

EditInline for GenericForiegnKey II

Author:
paltman
Posted:
June 29, 2008
Language:
Python
Version:
.96
Score:
5 (after 5 ratings)

This is an update to snippet 765 as I was having trouble getting it to work on branches/newforms-admin @ r7771.

There are just a few minor changes to the previous snippet, all simple stuff. I went ahead an added can_delete and can_order options that the previous snippet didn't include.

More details in blog post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from django.contrib.contenttypes.models import ContentType
from django.newforms.models import BaseModelFormSet, modelformset_factory, save_instance
from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
from django.db.models import ForeignKey
  
class GenericInlineFormset(BaseModelFormSet):
    """A formset for child objects related to a parent."""
    def __init__(self, data=None, files=None, instance=None, save_as_new=False):
        self.save_as_new = save_as_new
        self.instance = instance
        self.rel_name = '-'.join( ( self.model._meta.app_label, self.model._meta.object_name.lower(), self.ct.name,  self.obj_id.name ) )
        super(GenericInlineFormset, self).__init__(queryset=self.get_queryset(), data=data, files=files, prefix=self.rel_name)
  
    def get_queryset(self):
        if self.instance is None:
            return []
        return self.model._default_manager.filter( **{ 
                self.ct.name : ContentType.objects.get_for_model(self.instance),
                self.obj_id.name : self.instance.pk 
            })
  
    def save_new(self, form, commit=True):
        kwargs = {
            self.ct.get_attname(): ContentType.objects.get_for_model(self.instance).pk,
            self.obj_id.get_attname(): self.instance.pk,
        }
        new_obj = self.model(**kwargs)
        return save_instance(form, new_obj, commit=commit)
  
class GenericInlineModelAdmin(InlineModelAdmin):
    ct_field_name = None
    id_field_name = None
    formset = GenericInlineFormset
    can_delete = True
    can_order = True

    def get_formset( self, request, obj=None ):
        if self.declared_fieldsets:
            fields = flatten_fieldsets(self.declared_fieldsets)
        else:
            fields = None
  
        opts = self.model._meta
        ct = opts.get_field(self.ct_field_name)
        if not isinstance(ct, ForeignKey) or ct.rel.to != ContentType:
            raise Exception("fk_name '%s' is not a ForeignKey to ContentType" % (self.ct_field_name))
        obj_id = opts.get_field(self.id_field_name) # let the exception propagate
  
        FormSet = modelformset_factory(self.model, 
                                        form=self.form,
                                        formfield_callback=self.formfield_for_dbfield,
                                        formset=self.formset,
                                        extra=self.extra, 
                                        fields=fields, 
                                        can_delete=self.can_delete, 
                                        can_order=self.can_order,
                                        exclude=[ct.name, obj_id.name] )
        FormSet.ct = ct
        FormSet.obj_id = obj_id     
        return FormSet 

class GenericStackedInline(GenericInlineModelAdmin):
    template = 'admin/edit_inline/stacked.html'
 
class GenericTabularInline(GenericInlineModelAdmin):
    template = 'admin/edit_inline/tabular.html'

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

mikko (on July 22, 2008):

Nice!

from django.newforms.models needs to be from django.forms.models now in 1.0 alpha.

I would also change the default values for ct_field_name and id_field_name to the values they use in the docs, which means you don't have to set them when using admin unless you use other names of cource.

...
class GenericInlineModelAdmin(InlineModelAdmin):
    ct_field_name = 'content_type'
    id_field_name = 'object_id'
    ...

#

Please login first before commenting.