guettli
guettli

Reputation: 27806

Django Admin: Add text at runtime next to a field

I want to add a text next to a field of the django admin interface.

The warning needs to created at runtime inside a python method. I know python and the django ORM well, but I don't know how to get the text next the field.

The text should be a warning. Raising ValidationError in clean() is not a solution, since the user can't edit the page any more. It should be just a warning message.

Upvotes: 5

Views: 5082

Answers (3)

Martin Thurau
Martin Thurau

Reputation: 7644

I think the simplest way would be to override the specific admin page for that model. This is described here in the Django documentation. The template you need to override is probably change_form.html. Within these template displayed object is available in the template variable original.

I would add a method or property to you model, that generates and returns the error message and call this method from the template.

Edit: Have a look at contrib/admin/templates/admin/change_form.html there is a include for includes/fieldset.html that displays the the fields of the admin site. You could put some code there that chckes if the model has some special named attribute and if so it is displayed. You could them simply override that change_form.html for all models with your custom one.

Upvotes: 0

Anton Strogonoff
Anton Strogonoff

Reputation: 34032

You can use custom ModelForm subclass for the admin, adding help_text attribute for the field in question at its initialization, and style it appropriately.

# forms.py

class YourModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(YourModelForm, self).__init__(*args, **kwargs)
        self.fields['field_in_question'].help_text = generate_warning()

# admin.py

class YourModelAdmin(admin.ModelAdmin):
    form = forms.YourModelForm

    # And here you can specify custom CSS / JS which would make
    # `help_text` for that particular field look like a warning.
    # Or you can make it generic--say, style (and maybe reposition w/js) all tags
    # like <span class="warning"> that occur within the help text of any field.

    class Media:
        css = {"all": ("admin_warning.css", )}
        js = ("admin_warning.js", )

Upvotes: 7

zaan
zaan

Reputation: 897

If you want to do it in changelist view, you can write in model method, which returns string in format you want, and put name of that method in list_display in admin.

class MyModel(models.Model):
    myfield = models.CharField(max_length=100)

    def myfield_with_warning(self):
       return '%s - %s' % (self.myfield, '<span class="warn">My warning message</p>'
    myfield_with_warning.short_description = 'My field verbose name'
    myfield_with_warning.allow_tags = True

class MyModelAdmin(models.ModelAdmin):
    list_display = ('myfield_with_warning',)

If it's not what you need, write more precisely, where do you want to display warning message.

Upvotes: 0

Related Questions