Arka-cell
Arka-cell

Reputation: 906

Testing django geoodel with PointField results in TypeError

I have the following model:

from django.contrib.gis.db import models as geo_model

class UserAddress(geo_model.Model):
    user = geo_model.OneToOneField(
        User, on_delete=models.CASCADE, related_name="user_address"
    )
    coordinates = geo_model.PointField()
    address = geo_model.CharField(max_length=255)
    city = geo_model.CharField(max_length=20)
    country = geo_model.CharField(max_length=20)

    def __str__(self):
        return self.name

Now, I am trying to unit-test this model with pytest:

@pytest.mark.django_db
class test_user_address():
    user_address = UserAddress(
        user=User(),
        address="my address",
        city="Oran",
        country="Algeria",
        coordinates=(7.15, 35.0)
    )
    user_address.save()

But, this is resulting in the following error:

TypeError: Cannot set SeekerAddress SpatialProxy (POINT) with value of type: <class 'tuple'>

How should I pass datatype in coordinates?

Upvotes: 0

Views: 440

Answers (2)

Arka-cell
Arka-cell

Reputation: 906

So I found a temporary solution, I should have used Point instead of a tuple. I thought that PointField would accept a tuple and convert it to a Point.

Basically, here is what I did:

@pytest.mark.django_db
def test_user_address():
    user= User()
    user.save()
    user_address = UserAddress(
        user=user,
        address="my address",
        city="Oran",
        country="Algeria",
        coordinates=Point(7.15, 35.0)
    )
    user_address.save()

Upvotes: 0

May.D
May.D

Reputation: 1910

You need to use an instance of django.contrib.gis.geos Point.

So the correct code for your test is:

coordinates=Point(7.15, 35.0) # you can use tuple if you prefer but it's not mandatory

Upvotes: 1

Related Questions