简体   繁体   中英

Django - How to prepopulate admin form fields

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.

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.

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.

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.

I would also like to have a text field that automatically starts with something like "Hello my name is author".

Check out the docs at: http://docs.djangoproject.com/en/dev/ref/models/fields/#default

You could have a CharField() or TextField() in your model, and set this option, which will set the default text. 'default' can also be a callable function.

Something like: models.CharField(max_length=250, default="Default Text")

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

The slug handling is done with javascript.

So you have to override the templates in the admin and then populate the fields with javascript. The date thing should be trivial, but I dont know how you should get the logged in users name to the script (not that I have thought very hard but you get the drift :).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM