Opened 12 years ago

Last modified 12 years ago

#19330 closed Bug

change model instance with unique_together using modelform — at Initial Version

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

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.POSTid
cur_tag=Tag.objects.get(id=cur_id,user=request.user)
cur_req=request.POST.copy()
cur_requser=request.user.id
cur_form=Tagform(data=cur_req, instance=cur_tag)
if cur_form.is_valid():

cur_form.save()

Change History (0)

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