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

  • SEARCH
  • Home
  • 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 3597078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:04:19+00:00 2026-05-18T20:04:19+00:00

I have a ModelForm where i give users a chance to create a new

  • 0

I have a ModelForm where i give users a chance to create a new or edit an existing “Group” object. If “Edit” an existing is requested, i have it pre-populating the form data with the existing information. When the user goes to “Save” an edit on an existing object, i get this error:

“Group with this Name already exists.”

How can i tell Django to “Update” the object instead of attempting to create a new one with the same name?

MODEL

class Analyst(models.Model):
    first = models.CharField(max_length=32)
    last = models.CharField(max_length=32)
    def __unicode__(self):
        return "%s, %s" % (self.last, self.first)

class Alias(models.Model):
    alias = models.CharField(max_length=32)
    def __unicode__(self):
        return "%s" % (self.alias)

class Octet(models.Model):
    num = models.IntegerField(max_length=3)
    def __unicode__(self):
        return "%s" % (self.num)

class Group(models.Model):
    name = models.CharField(max_length=32, unique=True) #name of the group
    octets = models.ManyToManyField(Octet, blank=True) #not required
    aliases = models.ManyToManyField(Alias, blank=True) #not required
    analyst = models.ForeignKey(Analyst) #analyst assigned to group, required
    def __unicode__(self):
        return "%s" % (self.name)

VIEW

class GroupEditForm(ModelForm):
    class Meta:
        model = Group

def index(request):
    if request.method == 'GET':
        groups = Group.objects.all().order_by('name')
        return render_to_response('groups.html', 
                                  { 'groups': groups, }, 
                                  context_instance = RequestContext(request),
                                  )

def edit(request):
    if request.method == "POST":
        form = GroupEditForm(instance = Group.objects.get(name=request.POST['name']))
    elif request.method == "GET":
        form = GroupEditForm()

    return render_to_response('group_edit.html',
                             { 'form': form,  },
                             context_instance = RequestContext(request),
                             )

def save(request):
    if request.method == "POST":
        form = GroupEditForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return HttpResponseRedirect('/groups/')

    return render_to_response('group_save.html',
                               { 'test': form.errors,  })
  • 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-05-18T20:04:20+00:00Added an answer on May 18, 2026 at 8:04 pm

    You have to get the extining group object in the save action and bound the form to it.

    def save(request, id):
        if request.method == "POST":
           group = get_object_or_404(Group, pk=id)
           form = GroupEditForm(request.POST, instance=group) # A form bound to the POST data
           if form.is_valid():
              form.save(commit=True)
              return HttpResponseRedirect('/groups/')
    
        return render_to_response('group_save.html',
                               { 'test': form.errors,  })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a modelform which has one field that is a ForeignKey value to
I have a Modelform: class POwner4NewModel(ModelForm): class Meta: model = ProductOwner exclude = (o_owner,o_owner_desc,o_product_model,o_main_image,o_thumbnail,o_gallery_images,o_timestamp,o_status)
I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays
I have django ModelForm for model with ManyToManyField. I want to change widget for
Ok, here is the question. Imagine I have a ModelForm which have only two
I have a new web app that is packaged as a WAR as part
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
Have just started using Google Chrome , and noticed in parts of our site,

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.