2013-03-21 32 views
0

模型中,我有這樣的:Django的:不能分配 「(<節:XDS>)」: 「Announs.section」 必須是一個 「節」 實例

section = models.ForeignKey("Sections", verbose_name='Раздел')  

我的看法:

def announs_add(request): 
    if request.method == 'POST': 
     form = add_form(request.POST) 
     if form.is_valid(): 
      cd = form.cleaned_data 
      section_obj = get_object_or_404(Sections, id=cd['section']), 
      announ = Announs(section=section_obj) 
      announ.save() 
      form = add_form() 
    else: 
     form = add_form() 
    return render_to_response('announs/announs_add.html', 
    { 
    'form':form, 
    }, context_instance = RequestContext(request),) 

,如果我嘗試,例如添加的東西,我有這個:

Cannot assign "u'\u041d\u0443\u0436\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u0432\u043e\u0435\u0439 \u043f\u043e\u043c\u043e\u0449\u0438'": "Announs.section" must be a "Sections" instance. 

幫我解決這個問題。

回答

0

這種額外的逗號:

              # v-- this one 
section_obj = get_object_or_404(Sections, id=cd['section']), 

創建一個元組(包含節的1元組)。刪除逗號。

section_obj = get_object_or_404(Sections, id=cd['section']) 
+0

雅虎有用!!有用!!它很棒! – Unknown 2013-03-21 14:55:27

相關問題