Ticket #6075: newforms-admin_max_num.diff
File newforms-admin_max_num.diff, 2.2 KB (added by , 17 years ago) |
---|
-
django/contrib/admin/options.py
6 6 from django.contrib.admin.util import get_deleted_objects 7 7 from django.core.exceptions import ImproperlyConfigured, PermissionDenied 8 8 from django.db import models 9 from django.db.models.query import Q 9 10 from django.http import Http404, HttpResponse, HttpResponseRedirect 10 11 from django.shortcuts import get_object_or_404, render_to_response 11 12 from django.utils.html import escape … … 720 721 model = None 721 722 fk_name = None 722 723 extra = 3 724 max_num = None 723 725 template = None 724 726 verbose_name = None 725 727 verbose_name_plural = None … … 734 736 if self.verbose_name_plural is None: 735 737 self.verbose_name_plural = self.model._meta.verbose_name_plural 736 738 739 def get_extra_max(self, obj): 740 """Returns how many extra fields should be added. 741 with the constrain to never add more than max_num.""" 742 # get the foreign key to lookup the name 743 fk_parent=forms.models.get_foreign_key(self.parent_model, self.model, self.fk_name) 744 entries_num = len(self.model.objects.filter( apply(Q, (), { fk_parent.name: obj } ) ) ) 745 if self.max_num: 746 current_extra = self.max_num - entries_num 747 else: 748 current_extra = self.extra 749 if current_extra > self.extra: 750 current_extra = self.extra 751 return current_extra 752 737 753 def formset_add(self, request): 738 754 """Returns an InlineFormSet class for use in admin add views.""" 739 755 if self.declared_fieldsets: … … 748 764 fields = flatten_fieldsets(self.declared_fieldsets) 749 765 else: 750 766 fields = None 751 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self. extra)767 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.get_extra_max(obj)) 752 768 753 769 def fieldsets_add(self, request): 754 770 if self.declared_fieldsets: