Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom fields to InlineFormsets?

I'm trying to add custom fields to an InlineFormset using the following code, but the fields won't show up in the Django Admin. Is the InlineFormset too locked down to allow this? My print "ding" test fires as expected, I can print out the form.fields and see them all there, but the actual fields are never rendered in the admin.

admin.py

from django.contrib import admin
import models
from django.forms.models import BaseInlineFormSet
from django import forms
from forms import ProgressForm
from django.template.defaultfilters import slugify

class ProgressInlineFormset(BaseInlineFormSet):
    def add_fields(self, form, index):
        print "ding"
        super(ProgressInlineFormset, self).add_fields(form, index)
        for criterion in models.Criterion.objects.all():
            form.fields[slugify(criterion.name)] = forms.IntegerField(label=criterion.name)

class ProgressInline(admin.TabularInline):
    model = models.Progress
    extra = 8
    formset = ProgressInlineFormset

class ReportAdmin(admin.ModelAdmin):
    list_display = ("name", "pdf_column",)
    search_fields = ["name",]
    inlines = (ProgressInline,)

admin.site.register(models.Report, ReportAdmin)
like image 880
Soviut Avatar asked Feb 09 '09 00:02

Soviut


People also ask

How do I add custom fields to a form?

Leaving the Create new field dialog box will return you to the Insert fields dialog box. Any custom fields that were just added will be automatically marked in the field list to be inserted into the form. Click Insert to insert the marked fields into the selected region of the form.

How do I create a custom field in Salesforce?

Click the Create new field button above the list to initiate the process of creating a custom field. This will open the Create new field dialog box. If you do not see the Create new field button, you do not have the necessary permissions to use this feature. In the Create new field dialog box, enter the following information.

How many custom fields can I add to a table?

Note that there is currently a limit of 20 custom fields per table. Leaving the Create new field dialog box will return you to the Insert fields dialog box. Any custom fields that were just added will be automatically marked in the field list to be inserted into the form.

How do I create a custom data type for a field?

Select the data type for the new field. The available data types are checkbox, date, date time, decimal, number, picklist, and text. If you choose the text data type, you can also specify the maximum length of the text that can be entered in this field. If you choose the picklist data type, you can also select the set of valid values for the field.


2 Answers

I did it another way:

forms.py:

from django import forms
class ItemAddForm(forms.ModelForm):
    my_new_field = forms.IntegerField(initial=1, label='quantity')
    class Meta:
        model = Item

admin.py:

from django.contrib import admin
from forms import *
class ItemAddInline(admin.TabularInline):
    form = ItemAddForm

This works so far, I only need to override somehow the save method to handle this new field. See this: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#form . It says that by default Inlines use BaseModelForm, which is send to formset_factory. It doesn't work for me, tried to subclass BaseModelForm with errors (no attribute '_meta'). So I use ModelForm instead.

like image 63
alekwisnia Avatar answered Oct 14 '22 11:10

alekwisnia


You can do it by another way (Dynamic forms):

admin.py

class ProgressInline(admin.TabularInline):
    model = models.Progress
    extra = 8

    def get_formset(self, request, obj=None, **kwargs):
        extra_fields = {'my_field': forms.CharField()}
        kwargs['form'] = type('ProgressForm', (forms.ModelForm,), extra_fields)
        return super(ProgressInline, self).get_formset(request, obj, **kwargs)
like image 29
Sultan Avatar answered Oct 14 '22 11:10

Sultan