Search code examples
pythondjangounit-testinggeodjangofaker

GeoDjango and Mixer. 'PointField' has no attribute '_meta'


I have a Location model that defined (roughly) like this:

from django.contrib.gis.db import models


class Location(models.Model):
    address = models.CharField(max_length=255)
    gis = models.PointField(null=True)
    name = models.CharField(max_length=255)

Whenever I try to mixer.blend('app.Location') I get:

AttributeError: Mixer (app.Location): type object 'PointField' has no attribute '_meta'

It works when I blend it with the field set to None or Point, but I wonder if there is a way to set it up so it can do it on its own, especially since faker can generate this data.


Solution

  • mixer.register allows you to customize the way you want your data generated. In this case

    from django.contrib.gis.geos import Point
    
    
    mixer.register(
        'app.Location',
        gis=lambda: Point(
            float(mixer.faker.latitude()),
            float(mixer.faker.longitude()),
        ),
    )
    

    does the job.