Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to prepopulate admin form fields

People also ask

What is Fieldsets in Django admin?

I think you have misunderstood what fieldsets are. It's just a way for you to be able to group the fields on a Model Admin Page.

What is Admin ModelAdmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.

Can I use Django admin in production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.


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.


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


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