Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:40:27+00:00 2026-06-15T15:40:27+00:00

Code: Model: class Machines(models.Model): name = models.CharField(max_length=100, verbose_name=hostname, help_text=Host name of the machine) ip_addr

  • 0

Code:

Model:

class Machines(models.Model):
    name = models.CharField(max_length=100, verbose_name="hostname", help_text="Host name of the machine")
    ip_addr = models.CharField(max_length=50, primary_key=True, help_text="IP address of the machine")
.... have many other fields.

Form:
Using forms.ModelForm to create a form

View:

def save(request):
if request.method == "POST":
        ip = request.POST.get("ip")
        machine = get_object_or_404(Machines, ip_addr=ip)
        form = MachineForm(instance=machine, data=request.POST)
        if form.is_valid():
            if form.has_changed():
                form.save()
                context = {"message": "Updated successfully"}
            else:   
                context = {"message": "No data to update"}
    return render_to_response("edit.html", context, context_instance=RequestContext(request))

If I change “name” field, form.save() updates the current object properly. But, if I change “ip_addr” field which is primary key, form.save() creates two entries one with old primary key other with new primary key.

If we do same in MySQL(BTW, I am using MySQL as DB)

update machines_table set ip_addr="10.1.1.1" where ip_addr="10.1.1.2";

It works fine, there will not be any duplicate entry.

Can you please help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:40:28+00:00Added an answer on June 15, 2026 at 3:40 pm

    What you’re doing is updating private key. Django creates new instance in this case. It is is actually Django’s way to copy a record. The INSERT not UPDATE query is performed by ORM.

    If you change your code to:

    class Machines(models.Model):
        …
        ip_addr = models.CharField(max_length=50, unique=True, …
        …
    

    Django will interprete your edit action correctly as the primary key won’t change.

    Next time use django-debug-toolbar to see what query is performed by Django ORM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model: class Server(models.Model): serverId = models.IntegerField(verbose_name=_(serverId)) name = models.CharField(max_length=200, verbose_name=_(server_name)) ip
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I've two models: The first model: class Url < ActiveRecord::Base attr_accessible :code, :target, :user_id,
I'm try something from this comment idea Code Igniter Controller/Model name conflicts find class
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
i have the following code in the rails company model: class Company include Mongoid::Document
I'm using the Code First approach and have the following Model: public class Person
Below all my code, * Model * Below is Model code, public class MyViewModel
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
Here's sample model classes, which being use with Entity Framework Code First: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.