Rectangle 27 0

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

Did you restart the server? Depending on how you have your environment setup you might have to. I just tested the above (with my own model, but same thing...) and it worked fine.

Hmm, usually my changes show up without a restart (for instance, adding new fields etc) - Ive got an apache/mod_python setup that refreshes on each request..

Maybe there is a way we're creating our formsets differently? I use something like, LocFormSet = modelformset_factory(MyModel) pformset = LocFormSet(request.POST, request.FILES, queryset=MyModel.objects.order_by('name'))

I'm not sure what to tell you, "it works on my machine" :P What exactly happens? It just keeps showing a hidden field?

python - How to show hidden autofield in django formset - Stack Overfl...

python django formset
Rectangle 27 0

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

Thanks for the info on form = .. I'm not sure how I missed that in the docs! I'm still stuck with my original issue though - Edit no. 3 in the question illustrates the current code. Any ideas on how I could achieve that?

As Daniel said, you still need to move the locid declaration outside of the init method.

Thanks - the queryset now works, but I'm still stuck with not being able to see the locid! Declaring it exactly as above - locid is part of the class not init - and I have the self.fields in there as well - don't end up in locid being visible.

In fact, the self.fields complains about not having the locid key..

+1 for the straightforward efforts in your examples, Daniel.

python - How to show hidden autofield in django formset - Stack Overfl...

python django formset
Rectangle 27 0

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

There is a ticket filed (http://code.djangoproject.com/ticket/10427), which adds a "value" option to a template variable for a form. For instance, it allows,

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

python - How to show hidden autofield in django formset - Stack Overfl...

python django formset
Rectangle 27 0

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

python - How to show hidden autofield in django formset - Stack Overfl...

python django formset