Django - stockinfo_note.stock_id上的NOT NULL约束失败 - ForeignKey错误

时间:2017-04-05 19:42:02

标签: python django

目的和问题:

我想在同一个模板中放入两个表单并在一个视图中处理它们。我可以让模板显示两个表单但不提交它们(即处理提交)。当我提交时,我收到以下错误:

Exception Value: NOT NULL constraint failed: stockinfo_note.stock_id

'stock'是ForeignKey。我无法弄清楚如何在Views.py中获取函数stock_new来填充外键ID,这是我认为我需要做的就是克服上述错误。

Models.py:

class Stock(models.Model):
    '''
    Model representing the stock info.
    '''
    user = models.ForeignKey(User)
    ticker_code = models.CharField(max_length=10, null=True, blank=True)

    def __str__(self):
        return self.ticker_code

class Note(models.Model):
    '''
    Model representing the note.
    '''
    user = models.ForeignKey(User)
    text_note = models.TextField()
    stock = models.ForeignKey(Stock)

    def __str__(self):
        return self.text_note

Forms.py

class StockForm(forms.ModelForm):

    class Meta:
        model = Stock
        fields = ('ticker_code',)

class NoteForm(forms.ModelForm):

    class Meta:
        model = Note
        fields = ('text_note',)

Views.py

def stocknoter(request):
    stock = Stock.objects.filter(user=request.user)
    note = Note.objects.filter(user=request.user)

    context_dict = {'stock':stock, 'note':note}
    return render(request, "stocknoter/stock.html", context_dict)

def stock_new(request):
    if request.method == "POST":
        form1 = StockForm(request.POST)
        form2 = NoteForm(request.POST)
        if form1.is_valid() and form2.is_valid():
            stock = form1.save(commit=False)
            stock.user = request.user
            stock.save()

            note = form2.save(commit=False)
            note.user = request.user
            '''
            **THE PROBLEM IS AROUND HERE i think, how do i pass stock_id created 
            by form1 so form2 saves it as the foreignkey?**
            '''
            note.save()
            return redirect('stock')
    else:
        form1 = StockForm()
        form2 = NoteForm()
    return render(request, 'stocknoter/stock_edit.html', {'form1':form1, 'form2':form2})

如上所述,我认为问题在于后一部分(抱歉应该添加我是Django的新手)。我需要做什么来保存外键ID?

1 个答案:

答案 0 :(得分:0)

好的解决了这个问题。在stock_new我添加了:note.stock = stock

所以views.py现在看起来像这样:

def stocknoter(request):
    stock = Stock.objects.filter(user=request.user)
    note = Note.objects.filter(user=request.user)

    context_dict = {'stock':stock, 'note':note}
    return render(request, "stocknoter/stock.html", context_dict)

def stock_new(request):
    if request.method == "POST":
        form1 = StockForm(request.POST)
        form2 = NoteForm(request.POST)
        if form1.is_valid() and form2.is_valid():
            stock = form1.save(commit=False)
            stock.user = request.user
            stock.save()

            note = form2.save(commit=False)
            note.user = request.user

            note.stock = stock

            note.save()
            return redirect('stock')
else:
    form1 = StockForm()
    form2 = NoteForm()
return render(request, 'stocknoter/stock_edit.html', {'form1':form1, 'form2':form2})