# TypeError: float() argument must be a string or a real number, not 'F'

**URL:** <https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543>\
**Category:** Using the ORM\
**Created:** [November 22, 2023, 11:12am UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543 "2023-11-22T11:12:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirodil](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mirodil/32/10820_2.png) [@mirodil](https://forum.djangoproject.com/u/mirodil)\
**Post date:** [November 22, 2023, 11:12am UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/1 "2023-11-22T11:12:07Z")

</div>

Hi everyone, Is it possible to change type of F into float or can we get actual value in annotate() ?

```auto
from geopy.distance import distance
queryset.annotate(
                distance=distance(
                                   (latitude, longitude), 
                                   (F("address__latitude"), F("address__longitude"))
                                )
            ).filter(distance__lte=query_distance)

```

In this code I am filtering objects by distance, so `distance` is a method from geopy that accepts two tuple with lat1, long2 and long1, long2. However my second tuple got F()

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 22, 2023, 12:49pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/2 "2023-11-22T12:49:36Z")

</div>

Please post the full error message.

Is this a Django error or a database error?

Are you sure it’s the tuple that is incorrect? What is `query_distance`?

Is the `distance` function a database function? (Does it translate to SQL, or is it a python-based function?) If it’s a python function, then it can’t be used in the annotation clause.

---

<div class="post-metadata">

**Author:** ![mirodil](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mirodil/32/10820_2.png) [@mirodil](https://forum.djangoproject.com/u/mirodil)\
**Post date:** [November 22, 2023, 1:09pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/3 "2023-11-22T13:09:36Z")

</div>

```python
from geopy.distance import distance
from django_filters import rest_framework as filters

class TaskFilter(filters.FilterSet):
    min_price = filters.NumberFilter(field_name="budget", lookup_expr="gte")
    point = filters.CharFilter(method="filter_by_distance")

    class Meta:
        model = Task
        fields = ["point", "min_price"]

    def filter_by_distance(self, queryset, name, value):
        point = value.split(",")
        if len(point) == 2:
            latitude = float(point[0])
            longitude = float(point[1])
            query_distance = self.data.get("distance")
            queryset = queryset.annotate(
                distance=distance((latitude, longitude), (F("address__latitude"), F("address__longitude")))
            ).filter(distance__lte=query_distance)
        return queryset

```

> [@KenWhitesell](#):
>
> Is the `distance` function a database function?

No, it’s python-based method that comes from geopy package to calculate distance

> [@KenWhitesell](#):
>
> Are you sure it’s the tuple that is incorrect?

Yes, Because distance accepts float or real number type objects.

> [@KenWhitesell](#):
>
> If it’s a python function, then it can’t be used in the annotation clause.

Is there any way to make it work ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 22, 2023, 1:21pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/4 "2023-11-22T13:21:25Z")

</div>

> [@mirodil](#):
>
> Is there any way to make it work ?

No. (At least not directly)

The ORM works by creating an SQL statement that gets shipped off to the database, where it is parsed and executed.

The entire statement must rely upon functions available to the database engine. You can’t mix functions between what’s in the database and what only exists in your application.

What you would need to look for would be the equivalent PostGIS function to perform that calculation, or, create your own User-Defined Function to implement it.

---

<div class="post-metadata">

**Author:** ![mirodil](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mirodil/32/10820_2.png) [@mirodil](https://forum.djangoproject.com/u/mirodil)\
**Post date:** [November 22, 2023, 1:25pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/5 "2023-11-22T13:25:06Z")

</div>

There is an answer for this [question](https://stackoverflow.com/questions/62505949/how-to-do-arthmetic-on-django-f-types) like mine but I didn’t get it what about it and what user offers to do. Can you check it out ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 22, 2023, 1:26pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/6 "2023-11-22T13:26:12Z")

</div>

Different issue, and not relevant here. _Your_ root issue is that you’re trying to use a Python function in an sql statement. The “float” error is a symptom of the deeper problem. You’re trying to pass an `F` function to a python function that has no idea of what to do with it.

---

<div class="post-metadata">

**Author:** ![mirodil](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mirodil/32/10820_2.png) [@mirodil](https://forum.djangoproject.com/u/mirodil)\
**Post date:** [November 22, 2023, 1:33pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/7 "2023-11-22T13:33:34Z")

</div>

So is it impossible to extract that actual field value to pass it to python function in annotate?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 22, 2023, 1:42pm UTC](https://forum.djangoproject.com/t/typeerror-float-argument-must-be-a-string-or-a-real-number-not-f/25543/8 "2023-11-22T13:42:41Z")

</div>

Looking at this particular expression:

> [@mirodil](#):
>
> ```auto
> distance=distance(
> (latitude, longitude), 
> (F("address__latitude"), F("address__longitude"))
> )
> 
> ```

This portion of it on the right-hand side of the assignment operator:

```auto
distance(
  (latitude, longitude), 
  (F("address__latitude"), F("address__longitude"))
)

```

is going to be evaluated **before** it is constructed into an SQL statement and sent to the database. There is no field `address__latitude` to retrieve at this point in time, because the processing is still occurring in your application.

Logically, it’s no different than the expression:

> [@mirodil](#):
>
> `filter(distance__lte=query_distance)`

Where `query_distance` (the right-hand side of the assignment operation) is evaluated **before** the query is sent to the database.
