编辑表单未实例化

时间:2010-03-27 18:09:39

标签: django forms

我有两个这样的模型:

class OptionsAndFeatures(models.Model):
   options = models.TextField(blank=True, null=True)
   entertainment = models.TextField(blank=True, null=True)
   seats_trim = models.TextField(blank=True, null=True)
   convenience = models.TextField(blank=True, null=True)
   body_exterior = models.TextField(blank=True, null=True)
   lighting = models.TextField(blank=True, null=True)
   safety = models.TextField(blank=True, null=True)
   powertrain = models.TextField(blank=True, null=True)
   suspension_handling = models.TextField(blank=True, null=True)
   specs_dimensions = models.TextField(blank=True, null=True)
   offroad_capability = models.TextField(blank=True, null=True)
class Vehicle(models.Model):
   ...
   options_and_features = models.ForeignKey(OptionsAndFeatures, blank=True, null=True)

我在添加和编辑视图中都使用了OptionsAndFeatures类的模型表单。在添加视图中,它工作得很好。但编辑视图会将OptionsAndFeatures呈现为空白。编辑视图的代码如下:

def edit_vehicle(request, stock_number=None):   
   vehicle = get_object_or_404(Vehicle, stock_number=stock_number)

   if request.method == 'POST':
      # save info
   else:
       vehicle_form = VehicleForm(instance=vehicle)
       photos = PhotosFormSet(instance=vehicle)
       options = OptionsForm(instance=vehicle)

   #render_to_reponse

这可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您将Vehicle对象作为instance参数传递给可能基于OptionsAndFeatures类的表单。这不起作用 - 显然你需要一个OptionsAndFeatures对象。