Ticket #9025: django_nested_inlines.diff

File django_nested_inlines.diff, 4.4 KB (added by vo.sinh@…, 16 years ago)
  • django/contrib/admin/options.py

    diff -Naur --exclude='*.pyc' django/contrib/admin/options.py /usr/lib/python2.4/site-packages/django/contrib/admin/options.py
    old new  
    279279    def get_formsets(self, request, obj=None):
    280280        for inline in self.inline_instances:
    281281            yield inline.get_formset(request, obj)
    282            
     282            for nested in inline.inline_instances:
     283                yield nested.get_formset(request, obj)
     284                for subnested in nested.inline_instances:
     285                    yield subnested.get_formset(request, obj)
     286
    283287    def log_addition(self, request, object):
    284288        """
    285289        Log that an object has been successfully added.
     
    603607        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
    604608        media = self.media + adminForm.media
    605609
     610        nested_inlines = []
     611        subnested_inlines = []
     612        for inline in self.inline_instances:
     613            for nested in inline.inline_instances:
     614                nested_inlines.append(nested)
     615                for subnested in nested.inline_instances:
     616                    subnested_inlines.append(subnested)
     617        all_inlines = self.inline_instances + nested_inlines + subnested_inlines
     618
    606619        inline_admin_formsets = []
    607         for inline, formset in zip(self.inline_instances, formsets):
    608             fieldsets = list(inline.get_fieldsets(request, obj))
    609             inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
    610             inline_admin_formsets.append(inline_admin_formset)
    611             media = media + inline_admin_formset.media
     620        for formset in formsets:
     621            for inline in all_inlines:
     622                if inline.model == formset.model:
     623                    fieldsets = list(inline.get_fieldsets(request, obj))
     624                    inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
     625                    inline_admin_formsets.append(inline_admin_formset)
     626                    media = media + inline_admin_formset.media
     627                    break
    612628
    613629        context = {
    614630            'title': _('Change %s') % force_unicode(opts.verbose_name),
     
    759775    template = None
    760776    verbose_name = None
    761777    verbose_name_plural = None
     778    inlines = []
    762779
    763780    def __init__(self, parent_model, admin_site):
    764781        self.admin_site = admin_site
     
    769786            self.verbose_name = self.model._meta.verbose_name
    770787        if self.verbose_name_plural is None:
    771788            self.verbose_name_plural = self.model._meta.verbose_name_plural
    772    
     789        self.inline_instances = []
     790        for inline_class in self.inlines:
     791            inline_instance = inline_class(self.model, self.admin_site)
     792            self.inline_instances.append(inline_instance)
     793
    773794    def _media(self):
    774795        from django.conf import settings
    775796        js = []
  • django/contrib/admin/validation.py

    diff -Naur --exclude='*.pyc' django/contrib/admin/validation.py /usr/lib/python2.4/site-packages/django/contrib/admin/validation.py
    old new  
    137137        raise ImproperlyConfigured("'%s.formset' does not inherit from "
    138138                "BaseModelFormSet." % cls.__name__)
    139139
     140    # nested inlines = []
     141    if hasattr(cls, 'inlines'):
     142        check_isseq(cls, 'inlines', cls.inlines)
     143        for idx, inline in enumerate(cls.inlines):
     144            if not issubclass(inline, BaseModelAdmin):
     145                raise ImproperlyConfigured("'%s.inlines[%d]' does not inherit "
     146                        "from BaseModelAdmin." % (cls.__name__, idx))
     147            if not inline.model:
     148                raise ImproperlyConfigured("'model' is a required attribute "
     149                        "of '%s.inlines[%d]'." % (cls.__name__, idx))
     150            if not issubclass(inline.model, models.Model):
     151                raise ImproperlyConfigured("'%s.inlines[%d].model' does not "
     152                        "inherit from models.Model." % (cls.__name__, idx))
     153            validate_base(inline, inline.model)
     154            validate_inline(inline)
     155
     156
     157
     158
     159
    140160def validate_base(cls, model):
    141161    opts = model._meta
    142162
Back to Top