Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initial data for Django admin model add instance form?

How can I set an initial value of a field in the automatically generated form for adding a Django model instance, before the form is displayed? I am using Django 1.3.1.

My model is the following:

class Foo(models.Model):   title = models.CharField(max_length=50)   description = models.TextField() 

and the current admin form is really nothing special

class FooAdmin(admin.ModelAdmin):   ordering = ('title',) 

When I use the admin page to add a new instance of Foo, I get a nice form with empty fields for title and description. What I would like is that the description field is set with a template that I obtain by calling a function.

My current best attempt at getting there is this:

def get_default_content():   return 'this is a template for a Foo description'  class FooAdminForm(django.forms.ModelForm):    class Meta:       model = Foo    def __init__(self, *args, **kwargs):       kwargs['initial'].update({'description': get_default_content()})       super(FooAdminForm, self).__init__(self, *args, **kwargs)  class FooAdmin(admin.ModelAdmin):   ordering = ('title',)   form = FooAdminForm 

but if I try this I get this Django error:

AttributeError at /admin/bar/foo/add/     'FooForm' object has no attribute 'get' Request Method: GET Request URL:    http://localhost:8000/admin/bar/foo/add/ Django Version: 1.3.1 Exception Type: AttributeError Exception Value:    'FooForm' object has no attribute 'get' Exception Location: /www/django-site/venv/lib/python2.6/site-packages/django/forms/widgets.py in value_from_datadict, line 178 

I don't know what is wrong here, and what I should do to make it work. What I also find strange about this error (apart from the fact that I see it at all) is that there is no FooForm in my code at all?

like image 481
Martijn de Milliano Avatar asked Apr 03 '12 20:04

Martijn de Milliano


People also ask

How does Django model define default data?

You need to create an empty migration file and Do your stuff in operations block, as explained in docs. As well as changing the database schema, you can also use migrations to change the data in the database itself, in conjunction with the schema if you want.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How do I automatically register all models in Django admin?

To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface. Open admin.py file and add this code to it. This will fetch all the models in all apps and registers them with the admin interface.


1 Answers

Alasdair's approach is nice but outdated. Radev's approach looks quite nice and as mentioned in the comment, it strikes me that there is nothing about this in the documentation.

Apart from those, since Django 1.7 there is a function get_changeform_initial_data in ModelAdmin that sets initial form values:

def get_changeform_initial_data(self, request):     return {'name': 'custom_initial_value'} 
like image 191
Wtower Avatar answered Oct 06 '22 12:10

Wtower