Apreche
Apreche

Reputation:

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.

Upvotes: 35

Views: 28139

Answers (7)

Till Kolter
Till Kolter

Reputation: 662

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

Upvotes: 5

bjunix
bjunix

Reputation: 4518

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.

Upvotes: 30

sacabuche
sacabuche

Reputation: 2839

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.

Upvotes: 55

Carl Meyer
Carl Meyer

Reputation: 126531

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.

Upvotes: 13

Andrea Di Persio
Andrea Di Persio

Reputation: 3266

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

Upvotes: 4

anonymous coward
anonymous coward

Reputation: 12814

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")

Upvotes: 3

Mr Shark
Mr Shark

Reputation: 26468

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 :).

Upvotes: 2

Related Questions