开发者

django ModelForm "unique=True"

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)开发者_StackOverflow社区:
    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,  })


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜