无法更新django中的现有记录

时间:2014-05-30 15:45:50

标签: python mysql django

我需要更新数据库中的现有recode。我正在使用mysql。

这是我的模特课

class SuSurey(models.Model):
    #id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
    su_name = models.CharField(db_column='su_Name', max_length=30) # Field name made lowercase.
    description = models.CharField(db_column='Description', max_length=500, blank=True) # Field name made lowercase.
    duration = models.IntegerField(db_column='Duration') # Field name made lowercase.
    audience = models.CharField(db_column='Audience', max_length=20, blank=True) # Field name made lowercase.
    start_date = models.CharField(db_column='Start_date', max_length=50, blank=True) # Field name made lowercase.
    end_date = models.CharField(db_column='End_date', max_length=50, blank=True) # Field name made lowercase.
    create_date = models.CharField(db_column='Create_date', max_length=50, blank=True) # Field name made lowercase.
    update_date = models.CharField(db_column='Update_date', max_length=50, blank=True) # Field name made lowercase.
    create_by = models.IntegerField(db_column='Create_by', blank=True, null=True) # Field name made lowercase.
    update_by = models.IntegerField(db_column='Update_by', blank=True, null=True) # Field name made lowercase.
    def __unicode__(self):
        return self.su_name
    class Meta:
        managed = False
        db_table = 'su_surey'

我没有在我的网络应用程序中使用任何表单。在我的view.py中我有这个

if request.method == 'POST':
            date=request.POST.get('dates','').split('-')
            survey_data=SuSurey.objects.get(id=survey_id)
            survey_data.su_name=request.POST.get('Name','')
            survey_data.description=request.POST.get('Description','')
            survey_data.duration=request.POST.get('Duration','')
            survey_data.audience=request.POST.get('Audience','')
            survey_data.start_date=date[0]
            survey_data.end_date=date[1]
            survey_data.update_date=datetime.datetime.today()
            survey_data.update_by=request.session['user_login_data']['id']
            try:
                survey_data.full_clean()
                survey_data.save(update_fields=['su_name','description','duration','audience','start_date','end_date','update_date','update_by'])

                messages={'success':'Successfuly data added'}
                url = reverse('addQuestionSurveys', kwargs={'survey_id': survey_id })
                return HttpResponseRedirect(url)


            except ValidationError as e:
                messages={'error':e.messages}
                return render(request, 'pages/forms/newSurvey.html',{'dept_data': dept_data,'messages': messages},context_instance=RequestContext(request)) 

当我添加数据时,Django确实更新了由survey_id recode找到的重新编码。它会向database添加新的重新编码。我能解决这个问题吗?需要快速帮助谢谢。

0 个答案:

没有答案