无法使用django保存模型实例

时间:2014-04-02 13:21:06

标签: python django

我正在尝试根据按钮提交保存简单数据。但我不断收到以下错误:

TypeError at /exercise/1/
save() got an unexpected keyword argument 'commit'

models.py:

class StrategyHistory(models.Model):
    user = models.ForeignKey(User)
    strategy = models.ForeignKey(Strategies)

    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField()

    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        if not self.id:
            self.created = datetime.datetime.today()
        self.modified = datetime.datetime.today()
        return super(StrategyHistory, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.strategy.name

views.py:

def exercise_view(request, pk):
    template_name = 'mobileApp/page/exercise.html'
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('mobile_user_login'))

    strategy = Strategies.objects.get(pk=pk)

    context = {
        'strategy':strategy,
    }

    if request.method == 'POST':
        strategyhistory = StrategyHistory()
        entry = strategyhistory.save(commit=False)
        entry.user = self.request.user
        entry.strategy = Strategies.objects.get(id=pk)
        entry.save()
        if pk < 5:
            return HttpResponseRedirect(reverse('mobile_exercise', kwargs={'pk':pk+1}))
        else:
            return HttpResponseRedirect(reverse('mobile_comeback_later'))

    return render_to_response(template_name,
    context,
    context_instance = RC( request, {} ))

HTML文件:

        <form action="" method="post">{% csrf_token  %}
            <a href="#" data-role="button" data-inline="true" data-rel="back" data-transition="flow" data-icon="refresh" data-theme="c">Prøv igen</a>
            <button type="submit" value="submit" data-inline="true" data-transition="flow" data-icon="check" data-theme="b">Fortsæt</button>
        </form>

我正在运行Django 1.6.2

2 个答案:

答案 0 :(得分:1)

您的观点使用StrategyHistory模型不正确。这就是形式的使用。这是正确的。

def exercise_view(request, pk):
    template_name = 'mobileApp/page/exercise.html'
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('mobile_user_login'))

    strategy = Strategies.objects.get(pk=pk)

    context = {
        'strategy':strategy,
    }

    if request.method == 'POST':
        strategyhistory = StrategyHistory.objects.create(
            user=request.user,
            strategy=strategy)
        if pk < 5:
            return HttpResponseRedirect(reverse('mobile_exercise', kwargs={'pk': pk + 1}))
        else:
            return HttpResponseRedirect(reverse('mobile_comeback_later'))

    return render_to_response(template_name,
                              context,
                              context_instance = RC( request, {} ))

此外,您无法在该视图中使用self.request。没有自我声明。

答案 1 :(得分:0)

可能这就是你想要的:

class StrategyHistory(models.Model):
    user = models.ForeignKey(User)
    strategy = models.ForeignKey(Strategies)

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)


    def __unicode__(self):
        return self.strategy.name