Quantcast
Channel: Using Factory Boy with GeoDjango PointFields - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by mathias.lantean for Using Factory Boy with GeoDjango PointFields

$
0
0

Fussy is about to be deprecated as Factory Boy documentation says.

Now that FactoryBoy includes the factory.Faker class, most of these built-in fuzzers are deprecated in favor of their Faker equivalents.

As @Steven B said, you must create your own provider. I did some changes to his code in order to make the provider as generic as possible.

class DjangoGeoPointProvider(BaseProvider):    def geo_point(self, **kwargs):        kwargs['coords_only'] = True        faker = factory.Faker('local_latlng', **kwargs)        coords = faker.generate()        return Point(x=float(coords[1]), y=float(coords[0]), srid=4326)

Note: coords_only must be always true because we just need lat and long values, without any extra metadata.

Finally, it is like using the local_latlngprovider or any of the built-in providers. Here is a full example:

class TargetFactory(factory.django.DjangoModelFactory):    factory.Faker.add_provider(DjangoGeoPointProvider)    class Meta:        model = Target    radius = factory.Faker('random_int', min=4500, max=90000)    location = factory.Faker('geo_point', country_code='US')

Note: 'US' is the default country code, it could be omitted in this example, but you could use any of the other specified countries code in Faker doc.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images