我有两个这样的模型:
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
这可能是什么问题?
答案 0 :(得分:0)
您将Vehicle
对象作为instance
参数传递给可能基于OptionsAndFeatures
类的表单。这不起作用 - 显然你需要一个OptionsAndFeatures对象。