Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show hidden autofield in django formset
    text
    copied!<p>A Django autofield when displayed using a formset is hidden by default. What would be the best way to show it?</p> <p>At the moment, the model is declared as,</p> <pre><code>class MyModel: locid = models.AutoField(primary_key=True) ... </code></pre> <p>When this is rendered using Django formsets, </p> <pre><code>class MyModelForm(ModelForm): class Meta: model = MyModel fields = ('locid', 'name') </code></pre> <p>it shows up on the page as,</p> <pre><code>&lt;input id="id_form-0-locid" type="hidden" value="707" name="form-0-locid"/&gt; </code></pre> <p>Thanks.</p> <hr> <p><strong>Edit</strong></p> <p>I create the formset like this -</p> <pre><code>LocFormSet = modelformset_factory(MyModel) pformset = LocFormSet(request.POST, request.FILES, queryset=MyModel.objects.order_by('name')) </code></pre> <hr> <p><strong>Second Edit</strong></p> <p>Looks like I'm not using the custom form class I defined there, so the question needs slight modification..</p> <p><em>How would I create a formset from a custom form (which will show a hidden field), as well as use a custom queryset?</em></p> <p>At the moment, I can either inherit from a BaseModelFormSet class and use a custom query set, or I can use the ModelForm class to add a custom field to a <strong>form</strong>. Is there a way to do both with a formset?</p> <hr> <p><strong>Third Edit</strong></p> <p>I'm now using,</p> <pre><code>class MyModelForm(ModelForm): def __init__(self, *args, **kwargs): super(MyModelForm, self).__init__(*args, **kwargs) locid = forms.IntegerField(min_value = 1, required=True) self.fields['locid'].widget.attrs["type"] = 'visible' self.queryset = MyModel.objects.order_by('name') class Meta: model = MyModel fields = ('locid', 'name') LocFormSet = modelformset_factory(MyModel, form = MyModelForm) pformset = LocFormSet() </code></pre> <p>But this still doesn't </p> <ul> <li>Show locid</li> <li>Use the custom query that was specified.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload