Math opertation in template

Is there a possibility to convert string to integer to perform math operation in django templates?
I have a querry that returns Frequency from model (in model frequency is defined as Integer) however querry returns a string - is it a possibility to conver Frequency to an integer and then divide it by any number e.g {{int(Frequency)/1000}}

If you really wanted to do it in the template, you could probably create a custom template tag or filter to do this.

However, it’s probably more in the Django-way of doing things by creating a model method for that field, and then render the results of that model method in your template.

See the Behind the scenes block in the variables section of the template docs.

Ok, so how it can be done in the view?
Lets assume I have a custom get_queryset methood defined in the view which retuns following table:
Name Type Freq
Point1 A 100
Point1 B 500000
Point2 B 10000
Point2 A 50
Point3 B 100000

and now each of location B i would like to divide by specific value.

is it the right way to do that as shown below with an additional function which do the math:

 def get_queryset(self):
    result =....
    result_div = div(result)
    return result_div

 def div(get_queryset_data):
    for n in get_queryset_data:
          if (n.Type = "B"):
              n.Type = int(n.Freq) /1000

Are you returning a model, or is this the results of a raw query?

(Seeing the actual view would be helpful here. The answer may vary based upon the specifics of your implementation.)

class Airport_Navigation_Aid(models.Model):
    airport =  models.ForeignKey(Airport, on_delete=models.PROTECT, null= True, blank=True)
    Navi_name = models.CharField(max_length=150)
    Ident = models.CharField(max_length=6)
    Name = models.CharField(max_length=100)
    Type = models.CharField(max_length=7, choices=model_choices.NAVI_NAME, default='VOR')
    Frequency = models.IntegerField()
    Elevation = models.IntegerField()

view:

class Airport_Navigation_AidDetailListView(ListView):
    model = Airport_Navigation_Aid
    template_name = 'airports/navilist.html'

def get_queryset(self):
    navigation_list = Airport_Navigation_Aid.objects.all()
    return navigation_list.filter(airport_id = self.kwargs['airport_id'])

In your model, add something like the following method:

def get_frequency_display(self):
    if <whatever condition determines whether the division is to be done>:
        return self.Frequency / 1000
    else:
        return self.Frequency

which is then used in your template as:
{{ row.get_frequency_display }}, assuming “row” is your iteration variable in the template.

You may need to change the return value (either by formatting or doing a decimal division, or both) depending on whether you want something like 1200 / 1000 to be displayed as 1.200 or 1.2, or 2000 / 1000 to be displayed as 2, 2.0, or 2.000.