1

I have a ModelForm:

class SomeModelForm(forms.ModelForm):
    class Meta:
        model = SomeModel

    def __init__(self, *args, **kwargs):
        super(ApiBackendConfigForm, self).__init__(*args, **kwargs)
        if kwargs['instance'].name == u'Some_name':
            self.fields['method_config'] = forms.URLField()

and ModelAdmin:

class SomeAdmin(admin.ModelAdmin):
    form = SomeModelForm
    list_display = ('name', 'alias', 'is_enabled', )
    list_editable = ('is_enabled', )
    readonly_fields = ('name', 'alias', )

First question, method_config field is not displayed. I know, that it's not in list_display, but if I add it to list_display, then it causes an error.

And second main question: How can I add some link to other ModelAdmin?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
rva4
  • 43
  • 1
  • 4
  • I don't think you're showing enough information. What do you expect `method_config` to display when you add it to `list_display`. Where do you want to display the link, and what exactly do you want to link to? – Alasdair Oct 16 '15 at 11:26
  • @Alasdair, `method_config` should display a link to new ModelAdmin that is not written yet. – rva4 Oct 16 '15 at 11:37
  • depending on object.name – rva4 Oct 16 '15 at 11:44
  • Do you want there to be a form field when editing an item, or just a link when displaying the overview? I still don't understand what url you want to link to. – Alasdair Oct 16 '15 at 13:56

1 Answers1

0

Modifing self.fields might not be thread-safe. This means that if you modify self.fields on first request, all other requests will get that modified version.

Check ModelAdmin.get_fields(request, obj=None) method for changing which fields to display on the fly. Works on forms in changeview. If you want to display custom field in changelist view, just modify list_display wih method name instead of field.

For example:

class SomeAdmin(admin.ModelAdmin):
    list_display = ('name', 'alias', 'is_enabled', 'show_method_config', )
    list_editable = ('is_enabled', )
    readonly_fields = ('name', 'alias', )

    def show_method_config(self,obj):
        return getattr(obj,"method_config","") if obj.name == u"Some_name" else ""
    show_method_config.short_description = _(u"Method config")
    show_method_config.admin_order_field = "method_config"
    show_method_config.allow_tags = True

    def get_fields(self, request, obj=None):
        fields = super(SomeAdmin, self).get_fields(request, obj)
        if obj and obj.name == u"Some_name":
            fields.append("method_config")
        return fields
pista329
  • 691
  • 8
  • 14
  • `method_config` is not a model field – rva4 Oct 16 '15 at 13:01
  • it's not displaying anything – rva4 Oct 16 '15 at 14:24
  • If you want to produce url address to certain model view, try `from django.core.urlresolvers import reverse` `reverse("admin:{{ app_label }}_{{ model_name }}_add")` Check [Django admin site docs](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#reversing-admin-urls) for all available routes. – pista329 Oct 17 '15 at 17:13