Faker already has some nice geo features. You can create you own provider to make it work for Django, for example:
import factory
from faker.providers import BaseProvider
from django.contrib.gis.geos import Point
class DjangoGeoLocationProvider(BaseProvider):
countries = ['NL', 'DE', 'FR', 'BE']
# uses faker.providers.geo
def geolocation(self, country=None):
country_code = country or factory.Faker('random_element', elements=self.countries).generate()
faker = factory.Faker('local_latlng', country_code=country_code, coords_only=True)
coords = faker.generate()
return Point(x=float(coords[1]), y=float(coords[0]), srid=4326)
Which will generate Django compatible Points for the given countries.
After registering it:
factory.Faker.add_provider(DjangoGeoLocationProvider)
You can use it in your DjangoModelFactory like any of the built-in providers:
from factory.django import DjangoModelFactory as Factory
class PlaceFactory(Factory):
class Meta:
model = Place
location = factory.Faker('geolocation')
# or
another_location = factory.Faker('geolocation', country='NL')