openwisp / django-rest-framework-gis

Geographic add-ons for Django REST Framework. Maintained by the OpenWISP Project.
http://openwisp.org
MIT License
1.08k stars 200 forks source link

How to enable required=False for GeometryField? #51

Closed jab3z closed 9 years ago

jab3z commented 9 years ago

Hi,

I want to be able to update the location of an user without having the model itself managed by GeoManager, so I've created a ono to one relation with a geo model. For the sake of simplicity I want to update the location of the user in the same api call where I'm updating user details. I've added GeometryField on the User ModelSerializer with params required=False and allow_null=True, but still on POST/PATCH/PUT I have the "This field is required." for the geo field. How I can literally disable this?

Regards, Dacian

nemesifier commented 9 years ago

hi @jab3z,

Try this:

class MyGeoSerializer(GeoFeatureModelSerializer):
    # ... fields ...
    class Meta:
        model = MyModel
        geo_field = 'geometry'
        read_only_fields = ['geometry']

It should work, I tried it just now.

jab3z commented 9 years ago

Hi @nemesisdesign,

But I'm not inheriting from GeoFeatureModelSerializer, I don't see how this would work. Here's a draft of my code:

class Answer(models.Model):
    answer = models.TextField(_('answer'))

        @property
        def location(self):
            return location.location

        @location.setter
         def location(self, location):
             user_location = self.get_location_model()
             user_location.location = location
             user_location.save()

 class AnswerLocation(AbstractLocationModel):
     answer = models.OneToOneField(Answer, related_name='answer_location')

class AnswerSerializer(serializers.ModelSerializer):
    location = GeometryField(required=False, allow_null=True)
nemesifier commented 9 years ago

The code looks incomplete to me.

Which model does AnswerSerializer use? On the two models I do not see any geodjango field.

Give us more info.

jab3z commented 9 years ago

Hi,

Sorry, you are right. I missed to add that. There is location field on AnswerLocation model and AnswerSerializer it's using Answer model.

Here is how AbstractLocationModel looks like:

    location = models.PointField(
        srid=4326, verbose_name=_('location'), blank=True, null=True)
    objects = models.GeoManager()
nemesifier commented 9 years ago

So you do not want location to be read only, but you want it to be writable and optional, right?

jab3z commented 9 years ago

Yes. I want to be readable on GET and updatable on POST/PATCH, and optional indeed.

LE: actually, right now it's readable on GET and updatable on POST/PATCH, but is not optional on POST.

nemesifier commented 9 years ago

I looked in and it seems there's no specific unit test for this case. It seems nobody has implemented this use case. I guess everybody is using the GeoSerializer, which allows partial PATCH requests omitting the geo field.

If you have time you could write a failing test with your specific case and implement a solution.

Federico

nemesifier commented 9 years ago

hey @jab3z, it seems @nmandery has sent a patch in #53 to add this feature