Ticket #6075: newforms-admin_max_num_6075_v7363.diff
File newforms-admin_max_num_6075_v7363.diff, 2.3 KB (added by , 17 years ago) |
---|
-
django/contrib/admin/options.py
7 7 from django.contrib.admin.util import get_deleted_objects 8 8 from django.core.exceptions import ImproperlyConfigured, PermissionDenied 9 9 from django.db import models, transaction 10 from django.db.models.query import Q 10 11 from django.http import Http404, HttpResponse, HttpResponseRedirect 11 12 from django.shortcuts import get_object_or_404, render_to_response 12 13 from django.utils.html import escape … … 680 681 model = None 681 682 fk_name = None 682 683 extra = 3 684 max_num = None 683 685 template = None 684 686 verbose_name = None 685 687 verbose_name_plural = None … … 694 696 if self.verbose_name_plural is None: 695 697 self.verbose_name_plural = self.model._meta.verbose_name_plural 696 698 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 697 713 def get_formset(self, request, obj=None): 698 714 """Returns a BaseInlineFormSet class for use in admin add/change views.""" 699 715 if self.declared_fieldsets: 700 716 fields = flatten_fieldsets(self.declared_fieldsets) 701 717 else: 702 718 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)) 704 720 705 721 def get_fieldsets(self, request, obj=None): 706 722 if self.declared_fieldsets: