2014-11-24 81 views
0

型號:Django的預填充表單上更新記錄提交

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():

+0

表單上的錯誤是什麼?你可以用''{{form.errors}}''' – schillingt 2014-11-25 00:10:31

+0

來查看你的模板。錯誤是「這個名稱的位置已經存在」。每個需要唯一值的字段都會顯示錯誤。這就是爲什麼我想它是試圖創造一個新的記錄,而不是更新舊的。 – Jay42 2014-11-25 00:27:16

回答

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) 
+0

這工作,非常感謝你。 – Jay42 2014-11-25 18:02:27