Model problem

I have a model that needs to show a form multiple times. I used it under the sample model. I seem to have a problem with the id of this model, which is also the primary key for the model.
I am pre-filling a set of forms with the data I want to modify.
But whenever I click the submit button it refreshes the page with an error saying "(Hidden field id) with this No already exists".
This error occurs specifically for the "id" field, which is hidden

<input type="hidden" id="id_form-0-id" value="2972" name="form-0-id"/>

      

This is a snippet of the template. (I got it from firebug) What could be the problem as the form is not valid? I cannot save data.

ProfilesFormSet = modelformset_factory(Profile,exclude = ( <items spearated by commas>), extra=0) 
profile_form_set = ProfilesFormSet(queryset = Profile.objects.filter(userprofile=userprofile).order_by('-modified_on')) 

      

This is a piece of code.

0


a source to share


2 answers


If you are using PostgreSQL and any version of Django prior to 1.1beta, and your model is not set by default, I think you are probably seeing an error related to inconsistent ordering of objects returned from the database (see tickets for Django Trac 9076 , 9758 , 10163 , etc.).

Try adjusting the default order for the model:



class Meta:
    ordering = ('some_field',)

      

See if that fixes it.

+1


a source


I believe this error is caused by one of the following:

  • The Django form object you use inside a formset does not include the primary key (id) of the model. However, since you are using modelformset_factory, this should not be the case (you also won't get this error message).

  • The HTML form in your template doesn't include the primary key, even as a hidden field. Make sure you have {{ form.id }}

    or something in this pattern inside the loop {{ for form in formset }}

    .



I can't think of any more reasons at the moment, but I'm sure they will all relate to the form returned by the POST from the browser client, the id field is missing somehow.

0


a source







All Articles