1

I am trying to update a record in my database using info provided by my website's front-end using Model.objects.update(**kwargs) however on running the code I get the following error:

"'Record' with this Id already exists."

I would have expected that using the "update" method instead of the "create" method would mean that it shouldn't matter / should be expected that there is already a record with the same ID.

def find_patient(request): my_form = ExampleForm() if request.method == "POST": my_form = ExampleForm(request.POST) if my_form.is_valid(): UserInfo.objects.update(**my_form.cleaned_data) else: print(my_form.errors) context = { 'form': my_form, 'data_input': DataInput.objects, 'sections': SECTION_CHOICES } return render(request, 'example.html', context) 

The resulting error looks tike this:

  • id
  • User info with this Id already exists.

  • Am I misunderstanding the use case for "update" and if so, how would I go about using kwargs to update all fields for a given record in my db?

    2
    • Your update aims to update all records, not just a single one. Since it includes the primary key, you thus aim to set all the ids to a specific value, which of course raises an error. Commented Aug 16, 2019 at 15:48
    • you are doing it wrong way please check this out stackoverflow.com/questions/4673985/… Commented Aug 16, 2019 at 15:49

    1 Answer 1

    2

    Your UserInfo.objects.update(**my_form.cleaned_data) aims to update all records in the database. Since your form includes a primary key (id), it will thus aim to update the id of all records to a given value. Since these values however should be unque, that means it will of course raise an error.

    You can prevent that by filtering before the update, like:

    UserInfo.objects.filter( pk=my_form.cleaned_data['id'] ).update(**my_form.cleaned_data)

    But updating a specific record is usually done differently: you usually pass the primary key in the URL. So the urls.py looks like:

    # app/urls.py from django.urls import path from app.views import find_patient urlpatterns = [ # ... path('updatepatient/<int:pk>/', views.find_patient), # ... ]

    Then in the view, we can pass the item to update to the form:

    # app/views.py from django.shortcuts import get_object_or_404, redirect def find_patient(request, pk): instance = get_object_or_404(UserInfo, pk=pk) if request.method == "POST": my_form = ExampleForm(request.POST, instance=instance) if my_form.is_valid(): my_form.save() return redirect('some-view-name') else: my_form = ExampleForm() context = { 'form': my_form, 'data_input': DataInput.objects.all(), 'sections': SECTION_CHOICES } return render(request, 'example.html', context)

    In case of a successful POST request, one usually redirects to a page. This is the well-known Post/Redirect/Get pattern [wiki].

    Sign up to request clarification or add additional context in comments.

    Comments

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.