2010-03-27 81 views
0

我有兩個模型是這樣的:編輯形式沒有被實例化

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

您正在將Vehicle對象作爲參數instance傳遞給基於OptionsAndFeatures類的表單。這是行不通的 - 顯然你需要一個OptionsAndFeatures對象。

+0

啊......感謝......我的錯誤在那裏。 – Stephen 2010-03-27 18:19:36