[Answer]-Unable to save model instance with django

1๐Ÿ‘

โœ…

Your view use StrategyHistory model incorrect. That is the use of forms. This is correct.

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, {} ))

Also you cannot use self.request in that view. There is no self declared.

0๐Ÿ‘

May be this is what you want:

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
๐Ÿ‘คshellbye

Leave a comment