views:

2043

answers:

5

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://www.mysite.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.

+2  A: 

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

Mr Shark
+2  A: 

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

anonymous coward
I don't think this would meet the requirements. The idea is to prepopulate one field based on the just-entered value of another, all in Javascript. Callable default values on the server side won't help with that.
Carl Meyer
@Carl - that makes sense, I just didn't read it like that. Thanks.
anonymous coward
+2  A: 

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

Andrea Di Persio
+1  A: 

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.

Carl Meyer
A: 

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.

sacabuche