Django - How to prepopulate admin form fields

22,323

Solution 1

I know that you can prepopulate some values via GET, it will be something like this

http://localhost:8000/admin/app/model/add/?model_field=hello

I got some problems with date fields but, maybe this could help you.

Solution 2

I recently used Django's ModelAdmin.get_form method for this purpose.

class MyModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        form = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['my_field_name'].initial = 'abcd'
        return form

Yout should be careful about side effects as you are manipulating the base_fields directly.

Solution 3

Django's built-in prepopulated_fields functionality is hardcoded to slugify, it can't really be used for more general purposes.

You'll need to write your own Javascript function to do the prepopulating. The best way to get it included in the admin page is to include it in the inner Media class of a custom Form or Widget. You'll then need to customize your ModelAdmin subclass to use the custom form or widget. Last, you'll need to render some inline Javascript along with each prepopulated field to register the onchange handler and tell it which other field to populate from; I would render this via the custom Widget. To make it nice and declarative you could use a custom ModelAdmin attribute (similar to prepopulated_fields), and override ModelAdmin.formfield_for_dbfield to create the widget and pass in the information about what field it should prepopulate from.

This kind of admin hacking is almost always possible, but (as you can tell from this convoluted summary) rarely simple, especially if you're making an effort to keep your code nicely encapsulated.

Solution 4

You can override the default django admin field by replacing it with a form field of your choice.

Check this : Add custom validation to the admin

Solution 5

I tried a few of these answers and none of them worked. I simply wanted to prepulate a field with another field from a related model. Taking this answer as a starting point, I finally tried to manipulate the model instance object (here obj) directly and it worked for me.

class MyModelAdmin(models.ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        form = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
        if not obj.some_model_field:
            obj.some_model_field = obj.related_model.prepopulating_model_field

        return form
Share:
22,323
Admin
Author by

Admin

Updated on June 14, 2020

Comments

  • Admin
    Admin almost 4 years

    I know that you can prepopulate admin form fields based on other fields. For example, I have a slug field that is automatically populated based on the title field.

    However, I would also like to make other automatic prepopulations based on the date. For example, I have an URL field, and I want it to automatically be set to http://example.com/20090209.mp3 where 20090209 is YYYYMMDD.

    I would also like to have a text field that automatically starts with something like "Hello my name is author" where author is the current user's name. Of course, I also want the person to be able to edit the field. The point is to just make it so the user can fill out the admin form more easily, and not just to have fields that are completely automatic.