<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Coolmike890</title>
    <description>The latest articles on DEV Community by Coolmike890 (@coolmike890).</description>
    <link>https://dev.to/coolmike890</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1301677%2Fb865c5b0-d500-4e6b-a18a-fa034749314f.png</url>
      <title>DEV Community: Coolmike890</title>
      <link>https://dev.to/coolmike890</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coolmike890"/>
    <language>en</language>
    <item>
      <title>Updating a Record Creates a new Record</title>
      <dc:creator>Coolmike890</dc:creator>
      <pubDate>Tue, 25 Jun 2024 19:02:13 +0000</pubDate>
      <link>https://dev.to/coolmike890/updating-a-record-creates-a-new-record-d7c</link>
      <guid>https://dev.to/coolmike890/updating-a-record-creates-a-new-record-d7c</guid>
      <description>&lt;p&gt;I wish to update a record in Django using a form, but instead it creates a new record. I know it has to do with using get(), but I don't know how to fix it. Here is the relevant code:&lt;/p&gt;

&lt;p&gt;my form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class  AddRecordForm(forms.ModelForm):

    class Meta:
        model = Model2
        fields = ('field1', 'field2')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;my Models:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Model2(models.Model):

    id = models.CharField(primary_key=True, max_length=12)
    created_at = models.DateTimeField(auto_now_add=True)
    field3 = models.ForeignKey(Model3,  on_delete=models.PROTECT, related_name='master')
    field4 = models.PositiveSmallIntegerField()

    class Meta:
        ordering = ['id']

    def __str__(self):
        return(f"{self.field3} ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Model1(models.Model):

    field1 = models.ForeignKey(Model2,  on_delete=models.PROTECT, related_name='list')
    created_at = models.DateTimeField(auto_now_add=True)
    field2 =  models.PositiveSmallIntegerField(null=True, blank=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and my view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def update_record(request, pk):


    record = Model2.objects.get(field2=pk)
    form = AddRecordForm(instance=record)
    if request.method == 'POST':
            form = AddRecordForm(request.POST, instance=record)
        if form.is_valid():
            form.save()
            messages.success(request, "Record updated successfully!")
        return render(request, 'home', {})
        context = {'form':form}
    return render(request, 'update_record.html', context)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any help would be very much appreciated. Thanks.&lt;/p&gt;

</description>
      <category>django</category>
    </item>
  </channel>
</rss>
