Django预先填充的表单,用于更新提交记录

时间:2014-11-24 23:52:55

标签: django forms model

模型:

class locations(models.Model):#table
    Name = models.CharField(max_length=50,default='Joes quick stop', unique=True)
    shop_code = models.CharField(max_length=5,default='AB005',unique=True)
    manager = models.ManyToManyField(users)
    ast_manager = models.ManyToManyField(users, blank=True, related_name='ast_mng')
    sales_manager = models.ManyToManyField(users, blank=True, related_name='sales_mng')

形式:

class locations(models.Model):
   Name = models.CharField(max_length=50,default='Joes quick stop', unique=True)
   shop_code = models.CharField(max_length=5,default='AB005',unique=True)
   manager = models.ManyToManyField(users)
   ast_manager = models.ManyToManyField(users, blank=True, related_name='ast_mng')
   sales_manager = models.ManyToManyField(users, blank=True, related_name='sales_mng')

class locationsForm(ModelForm):
    class Meta:
        model = locations

浏览

def locations(request):
    locations = locations.objects.values().filter(id=request.session['location'])
    data=locations[0]
    if request.method == 'GET':
            if request.session['Manager']== True:
                form = locationsForm(initial=data)
            context = {'locations': locations, 'form': form}
            return render(request, 'locations/locations.html', context)
    elif request.method == 'POST':
            form=locationsForm(request.POST, instance=data)
            if form.is_valid():
                cd=form.cleaned_data
                form.save()
                form = locationsForm()
                locations= locations.objects.values().filter(id=request.session['depot'])
                context = {'locations': locations}
                return render(request, 'locations/locations.html', context)
            else:
                context = {'locations': locations, 'form': form}
                return render(request, 'locations/locations.html', context) 

我正在尝试显示填充了相关数据的表单,但用户可以更改然后保存/更新表单。上面的代码很好地显示了包含相关数据的表单,但是当用户尝试提交它时,系统会尝试保存新记录而不是更新旧记录而失败。我永远不会过去if form.is_valid():

1 个答案:

答案 0 :(得分:1)

您的问题是您正在将locations个对象转换为词典列表。然后将字典作为instance传递给表单。

这是因为您在查询集上调用.values()方法。该方法返回一个特殊的ValuesQuerySet,它基本上看起来像一个字典列表。不是locations个对象的列表。

表单上的instance参数需要是对象,而不是字典。所以,只需删除.values()次调用,就可以了。像这样:

def locations(request):
    locations = locations.objects.filter(id=request.session['location'])
    first_location=locations[0]
    if request.method == 'GET':
        if request.session['Manager']== True:
            form = locationsForm(instance=first_location)
        context = {'locations': locations, 'form': form}
        return render(request, 'locations/locations.html', context)
    elif request.method == 'POST':
        form=locationsForm(request.POST, instance=first_location)
        if form.is_valid():
            cd=form.cleaned_data
            form.save()
            form = locationsForm()
            locations= locations.objects.values().filter(id=request.session['depot'])
            context = {'locations': locations}
            return render(request, 'locations/locations.html', context)
        else:
            context = {'locations': locations, 'form': form}
            return render(request, 'locations/locations.html', context)