Home > Software design >  Django/python retrieving integer value from models.integerfield
Django/python retrieving integer value from models.integerfield

Time:09-23

class Trainingvalue(models.Model):
        maximalvalue= models.PositiveIntegerField(null=True,blank=True)

How can I retrieve integer from maximal value, in order to use in a views.py function like to create another variable to render it to context dictionary?

def trainingplan(request, pk):
    training = Trainingvalue.objects.get(id=pk)
    sets1=[3,6]
    sets2=[3,6]
    sets3=[2,4]
    repsrange1=[18,30]
    repsrange2=[12,24]
    repsrange3=[10,20]

    u = Trainingvalue.objects.values('maximalvalue').get(id=1)
   
    firstweek= [u*0.55, u*0.60, u*0.65]
    secondweek=[u*0.70,u*0.75,u*0.8]
    thirdweek=[u*0.8,u*0.85,u*0.9]
    a = f'Week 1 will consist of {sets1[0]}, {sets1[1]} sets of total reps {repsrange1[0]},{repsrange1[1]},at {firstweek[0]} kg,  {firstweek[1]}kg,{firstweek[2]}kg'
    b=  f'Week 2 will consist of {sets2[0]}, {sets2[1]} sets of total reps {repsrange2[0]},{repsrange2[1]} at {secondweek[0]} kg,  {secondweek[1]}kg,{secondweek[2]}kg'
    c=  f'Week 3 will consist of {sets3[0]}, {sets3[1]} sets of total reps {repsrange3[0]},{repsrange3[1]} at {thirdweek[0]} kg,  {thirdweek[1]}kg,{thirdweek[2]}kg'

    context={'training': training, 'a':a,'b':b,'c':c,}
      
    return render(request, 'base/trainingplan.html', context )

I want to render integer from this field , but i keep getting unsupported operand type(s) for *: 'dict' and 'float' .

CodePudding user response:

u is a dict, get the value out of the dict first

CodePudding user response:

  z= Trainingvalue.objects.values('maximalvalue').get(id=pk)
    u = z.get('maximalvalue')

this is how this problem was solved . Thank you mate for your help, aprecciate it

  • Related