Ticket #4491: site-aware-modeladmin.diff

File site-aware-modeladmin.diff, 5.3 KB (added by jkocherhans, 17 years ago)

This breaks the way StackedInline and TabularInline work, but makes them more like ModelAdmin. Still thinking about this...

  • django/contrib/admin/options.py

     
    118118    fields = None
    119119    filter_vertical = ()
    120120    filter_horizontal = ()
     121    prepopulated_fields = {}
     122
    121123    def formfield_for_dbfield(self, db_field, **kwargs):
    122124        """
    123125        Hook for specifying the form Field instance for a given database Field
     
    155157                # Wrap the widget's render() method with a method that adds
    156158                # extra HTML to the end of the rendered output.
    157159                formfield = db_field.formfield(**kwargs)
    158                 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel)
     160                formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site)
    159161                return formfield
    160162
    161163        # For any other type of field, just call its formfield() method.
     
    175177    save_as = False
    176178    save_on_top = False
    177179    ordering = None
    178     prepopulated_fields = {}
    179180    inlines = []
    180181
    181     def __init__(self, model):
     182    def __init__(self, model, admin_site):
    182183        self.model = model
    183184        self.opts = model._meta
     185        self.admin_site = admin_site
    184186
    185187    def __call__(self, request, url):
    186188        # Check that LogEntry, ContentType and the auth context processor are installed.
     
    610612
    611613    def get_inline_formsets(self):
    612614        inline_formset_classes = []
    613         for opts in self.inlines:
     615        for opts in [obj(self.admin_site) for obj in self.inlines]:
    614616            inline = forms.inline_formset(self.model, opts.model, formfield_callback=opts.formfield_for_dbfield, fields=opts.fields, extra=opts.extra)
    615617            inline_formset_classes.append(inline)
    616618        return inline_formset_classes
     
    623625    ``model`` to its parent. This is required if ``model`` has more than one
    624626    ``ForeignKey`` to its parent.
    625627    """
    626     def __init__(self, model, name=None, extra=3, fields=None, template=None, raw_id_fields=None):
    627         self.model = model
    628         self.opts = model._meta
    629         self.name = name
    630         self.extra = extra
    631         self.fields = fields
    632         self.template = template or self.default_template
    633         self.verbose_name = model._meta.verbose_name
    634         self.verbose_name_plural = model._meta.verbose_name_plural
    635         self.prepopulated_fields = {}
    636         self.raw_id_fields = raw_id_fields or ()
     628    model = None
     629    name = None
     630    extra = 3
     631    template = None
     632    verbose_name = None
     633    verbose_name_plural = None
    637634
     635    def __init__(self, admin_site):
     636        self.admin_site = admin_site
     637        self.opts = self.model._meta
     638        if not self.verbose_name:
     639            self.verbose_name = self.model._meta.verbose_name
     640        if not self.verbose_name_plural:
     641            self.verbose_name_plural = self.model._meta.verbose_name_plural
     642
    638643class StackedInline(InlineModelAdmin):
    639     default_template = 'admin/edit_inline_stacked.html'
     644    template = 'admin/edit_inline_stacked.html'
    640645
    641646class TabularInline(InlineModelAdmin):
    642     default_template = 'admin/edit_inline_tabular.html'
     647    template = 'admin/edit_inline_tabular.html'
    643648
    644649class BoundInline(object):
    645650    def __init__(self, inline_admin, formset):
  • django/contrib/admin/widgets.py

     
    9191    This class is a wrapper whose __call__() method mimics the interface of a
    9292    Widget's render() method.
    9393    """
    94     def __init__(self, render_func, rel):
     94    def __init__(self, render_func, rel, admin_site):
    9595        self.render_func, self.rel = render_func, rel
     96        # so we can check if the related object is registered with this AdminSite
     97        self.admin_site = admin_site
    9698
    9799    def __call__(self, name, value, *args, **kwargs):
    98100        from django.conf import settings
    99101        rel_to = self.rel.to
    100102        related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower())
    101103        output = [self.render_func(name, value, *args, **kwargs)]
    102         if rel_to._meta.admin: # If the related object has an admin interface:
     104        if rel_to in self.admin_site._registry: # If the related object has an admin interface:
    103105            # TODO: "id_" is hard-coded here. This should instead use the correct
    104106            # API to determine the ID dynamically.
    105107            output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
  • django/contrib/admin/sites.py

     
    7777        for model in model_or_iterable:
    7878            if model in self._registry:
    7979                raise AlreadyRegistered('The model %s is already registered' % model.__name__)
    80             self._registry[model] = admin_class(model)
     80            self._registry[model] = admin_class(model, self)
    8181
    8282    def unregister(self, model_or_iterable):
    8383        """
Back to Top