Nested Serializers should allow None relations #384

Closed
oundead opened this Issue Nov 8, 2012 · 4 comments

Comments

Projects
None yet
3 participants
@oundead

oundead commented Nov 8, 2012

Edit: Fixed title to better represent the ticket. - @tomchristie, 8th Nov 2012

i have a code:

class CallUserSerializer(ModelSerializer):
    class Meta:
        model = Users
        fields = ("id","name","surname","patronymic","photo")

class CallSerializer(ModelSerializer):
    source=CallUserSerializer()
    target=CallUserSerializer()
    source_id = fields.IntegerField()
    target_id = fields.IntegerField()
    apikey = fields.Field(source="OPENTOK_API_KEY")

    class Meta:
        model = Calls

class CallResource( mixins.ListModelMixin, MultipleObjectAPIView):
    model = Calls
    serializer_class = CallSerializer
    queryset = Calls.objects.prefetch_related("target","source").order_by("status","date")
    paginate_by = 10   

    def get(self,request, *args, **kwargs):
        self.queryset = self.queryset.filter(Q(target=request.user)|Q(source=request.user))
        pk = request.QUERY_PARAMS.get('pk')
        if pk is not None:
            self.queryset = self.queryset.filter(pk=pk)
        return self.list(self, request, *args, **kwargs)

Serializer throws exception NoneType, when model field Calls.target or Calls source is None.
I fixed it with mixin:

class NullSerializerPatch(BaseSerializer):
    def field_to_native(self, obj, field_name):
        if obj is None:
            return None
        val = getattr(obj, self.source or field_name)
        if val is None:
            return None
        return super(NullSerializerPatch,self).field_to_native(obj, field_name)

....

class CallSerializer(ModelSerializer, NullSerializerPatch):
    ....
@iamit

This comment has been minimized.

Show comment Hide comment
@iamit

iamit Dec 22, 2012

Had the same problem, the NullSerializerPatch works for me!
@oundead: thanks for this patch!

iamit commented Dec 22, 2012

Had the same problem, the NullSerializerPatch works for me!
@oundead: thanks for this patch!

@oundead

This comment has been minimized.

Show comment Hide comment
@oundead

oundead Dec 29, 2012

After framework updating:

class NullSerializerPatch(BaseSerializer):
    def to_native(self, obj):
         return super(NullSerializerPatch, self).to_native(obj) if obj else None

May be you add this check to master?...

oundead commented Dec 29, 2012

After framework updating:

class NullSerializerPatch(BaseSerializer):
    def to_native(self, obj):
         return super(NullSerializerPatch, self).to_native(obj) if obj else None

May be you add this check to master?...

@tomchristie

This comment has been minimized.

Show comment Hide comment
@tomchristie

tomchristie Dec 29, 2012

Collaborator

@oundead @iamit master should now support nullable nested relationships. Includes a test, but confirmation from either of you would be great. Will push to PyPI in due course.

Collaborator

tomchristie commented Dec 29, 2012

@oundead @iamit master should now support nullable nested relationships. Includes a test, but confirmation from either of you would be great. Will push to PyPI in due course.

@oundead

This comment has been minimized.

Show comment Hide comment
@oundead

oundead Dec 29, 2012

Ok. It works... I tested this.

oundead commented Dec 29, 2012

Ok. It works... I tested this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment