Opened 12 years ago

Last modified 12 years ago

#19330 closed Bug

change model instance with unique_together using modelform — at Version 2

Reported by: molavy Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: modelform, unique_together, instance, save, change
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Claude Paroz)

I have Problem on changing a model with modelform and instance
model form return form validate error:
user with this title already exist

model:

class Tag(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=765)
    create_time = models.DateTimeField(null=True, blank=True)
    status = models.IntegerField(null=True, blank=True)
    color = models.CharField(max_length=765, blank=True)
    cnt_member = models.IntegerField(null=True, blank=True)
    cnt_items = models.IntegerField(null=True, blank=True)
    def __unicode__(self):
        return self.title+ " || "+self.user.username
    class Meta:
        db_table = u'tbl_tag'
        unique_together=("user","title")

form:

class Tagform(forms.ModelForm):
    
    class Meta:
        model=Tag
        exclude = ('status','create_time')

view:

def change_tag_title(request):
    if request.POST and request.is_ajax():
        cur_id=request.POST["id"]
        cur_tag=Tag.objects.get(id=cur_id,user=request.user)
        cur_req=request.POST.copy()
        cur_req['user']=request.user.id
        cur_form=Tagform(data=cur_req, instance=cur_tag)
        if cur_form.is_valid():
            cur_form.save()

Change History (2)

comment:1 by molavy, 12 years ago

db :sqlite

comment:2 by Claude Paroz, 12 years ago

Description: modified (diff)

Please use preview and proper formatting.

Note: See TracTickets for help on using tickets.
Back to Top