Ticket #6075: newforms-admin_max_num_6075_v7363.diff

File newforms-admin_max_num_6075_v7363.diff, 2.3 KB (added by Jakub Wiśniowski, 17 years ago)

Updated patch for django version 7363 (should work for versions from 7270)

  • django/contrib/admin/options.py

     
    77from django.contrib.admin.util import get_deleted_objects
    88from django.core.exceptions import ImproperlyConfigured, PermissionDenied
    99from django.db import models, transaction
     10from django.db.models.query import Q
    1011from django.http import Http404, HttpResponse, HttpResponseRedirect
    1112from django.shortcuts import get_object_or_404, render_to_response
    1213from django.utils.html import escape
     
    680681    model = None
    681682    fk_name = None
    682683    extra = 3
     684    max_num = None
    683685    template = None
    684686    verbose_name = None
    685687    verbose_name_plural = None
     
    694696        if self.verbose_name_plural is None:
    695697            self.verbose_name_plural = self.model._meta.verbose_name_plural
    696698
     699    def get_extra_max(self, obj):
     700        """Returns how many extra fields should be added.
     701        with the constrain to never add more than max_num."""
     702        # get the foreign key to lookup the name
     703        fk_parent=forms.models._get_foreign_key(self.parent_model, self.model, self.fk_name)
     704        entries_num = len(self.model.objects.filter( apply(Q, (), { fk_parent.name: obj } ) ) )
     705        if self.max_num:
     706            current_extra = self.max_num - entries_num
     707        else:
     708            current_extra = self.extra
     709        if current_extra > self.extra:
     710            current_extra = self.extra
     711        return current_extra
     712
    697713    def get_formset(self, request, obj=None):
    698714        """Returns a BaseInlineFormSet class for use in admin add/change views."""
    699715        if self.declared_fieldsets:
    700716            fields = flatten_fieldsets(self.declared_fieldsets)
    701717        else:
    702718            fields = None
    703         return _inlineformset_factory(self.parent_model, self.model, form=self.form, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.extra)
     719        return _inlineformset_factory(self.parent_model, self.model, form=self.form, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.get_extra_max(obj))
    704720
    705721    def get_fieldsets(self, request, obj=None):
    706722        if self.declared_fieldsets:
Back to Top