[Fixed]-Saving form data rewrites the same row

12👍

If you want to create a new row, create it. 🙂 Like

store = Store(submitter=submitter,
              name=form.cleaned_data['name'],
              st=form.cleaned_data['st'],
              store.visit_date=form.cleaned_data['visit_date'])
store.save()

Now you use get_or_create method which tries to find a row with given parameters, so that’s why you updating it. And this method throws an error when there are multiple rows, yes, it’s its normal behavior.

By the way it’s better to place this saving code in form’s method (save for example).

P. S. Just noticed you don’t have visit_date field in your model, I think you meant sub_date.

0👍

Instead of using get_or_create you can simply use create

Store.objects.create(
    submitter=submitter,
    name=form.cleaned_data['name'],
    st=form.cleaned_data['st'],
    visit_date=form.cleaned_data['visit_date']
)

More information about the differences can be found Django Model() vs Model.objects.create()

Leave a comment