How to show hidden autofield in django formset

淺唱寂寞╮ 提交于 2019-12-06 12:05:25

Try changing the default field type:

from django import forms
class MyModelForm(ModelForm):
  locid = forms.IntegerField(min_value=1, required=True)
  class Meta:
    model = MyModel
    fields = ('locid', 'name')

EDIT: Tested and works...

As you say, you are not using the custom form you have defined. This is because you aren't passing it in anywhere, so Django can't know about it.

The solution is simple - just pass the custom form class into modelformset_factory:

LocFormSet = modelformset_factory(MyModel, form=MyModelForm) 

Edit in response to update 3:

Firstly, you have the redefinition for locid in the wrong place - it needs to be at the class level, not inside the __init__.

Secondly, putting the queryset inside the form won't do anything at all - forms don't know about querysets. You should go back to what you were doing before, passing it in as a parameter when you instantiate the formset. (Alternatively, you could define a custom formset, but that seems like overkill.)

class MyModelForm(ModelForm):
    locid = forms.IntegerField(min_value=1, required=True)

    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        self.fields['locid'].widget.attrs["type"] = 'visible'
    class Meta:
        model = MyModel
        fields = ('locid', 'name')

LocFormSet = modelformset_factory(MyModel, form = MyModelForm)
pformset = LocFormSet(request.POST, request.FILES,
                      queryset=MyModel.objects.order_by('name')))

Okay, none of the approaches above worked for me. I solved this issue from the template side, finally.

to be shown. This is available as a patch, which can be installed in the SVN version of django using "patch -p0 file.patch"

  • Remember, the {{form.locid.value}} variable will be used in conjunction with the invisible form - otherwise, the submit and save operations for the formset will crash.

  • This is Not the same as {{form.locid.data}} - as is explained in the ticket referred to above.

The reason that the autofield is hidden, is that both BaseModelFormSet and BaseInlineFormSet override that field in add_field. The way to fix it is to create your own formset and override add_field without calling super. Also you don't have to explicitly define the primary key.

you have to pass the formset to modelformset_factory:

    LocFormSet = modelformset_factory(MyModel, 
           formset=VisiblePrimaryKeyFormSet)

This is in the formset class:

from django.forms.models import BaseInlineFormSet, BaseModelFormSet, IntegerField
from django.forms.formsets import BaseFormSet

class VisiblePrimaryKeyFormset(BaseModelFormSet):
    def add_fields(self, form, index):
        self._pk_field = pk = self.model._meta.pk
        if form.is_bound:
            pk_value = form.instance.pk
        else:
            try:
                pk_value = self.get_queryset()[index].pk
            except IndexError:
                pk_value = None
        form.fields[self._pk_field.name] = IntegerField( initial=pk_value,
                 required=True) #or any other field you would like to display the pk in
        BaseFormSet.add_fields(self, form, index) # call baseformset which does not modify your primary key field
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!