Relation nested on get but just id or url on create

2,999 views
Skip to first unread message

Sven Mäurer

unread,
Mar 20, 2015, 9:23:40 AM3/20/15
to django-res...@googlegroups.com
I want to make a serializer which serializes a related object on get but for creation I just need the id. It sounds very simple for me and may I have a blocker in my brain.

Ion Scerbatiuc

unread,
Mar 20, 2015, 9:28:31 AM3/20/15
to django-res...@googlegroups.com
You should be able to accomplish what you want by specifying all the other fields as read_only=True.

class MySerializer(serializers.Serializer):
    id = serializers.CharField()  # or whatever type your id is
    related_field = serializer.HyperlinkedRelatedField(read_only=True, view_name='whatever')

Does this solve your issue?

On Fri, Mar 20, 2015 at 6:23 AM, Sven Mäurer <maeure...@gmail.com> wrote:
I want to make a serializer which serializes a related object on get but for creation I just need the id. It sounds very simple for me and may I have a blocker in my brain.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Filipe Ximenes

unread,
Mar 20, 2015, 9:31:34 AM3/20/15
to Django Rest Framework
Is making two serializers an option?
There could be an mixin with the shared data and each one (the create one and the get one) could implement its customisations.

On Fri, Mar 20, 2015 at 10:23 AM, Sven Mäurer <maeure...@gmail.com> wrote:
I want to make a serializer which serializes a related object on get but for creation I just need the id. It sounds very simple for me and may I have a blocker in my brain.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

Sven Mäurer

unread,
Mar 20, 2015, 9:45:21 AM3/20/15
to django-res...@googlegroups.com
class BuildingSerializer(serializers.ModelSerializer):
campus = FlatCampusSerializer(read_only=True)
campus_id = PrimaryKeyRelatedField(queryset=Campus.objects.all(), write_only=True)

class Meta:
model = Building
fields = ('id', 'url', 'name', 'campus', 'campus_id')


This is what I have at the moment. I would than have to change the campus_id into an appropriate campus but I thought it could go easier.

Sven Mäurer

unread,
Mar 21, 2015, 7:12:26 AM3/21/15
to django-res...@googlegroups.com
Could explain it to me based on my newest post please?


Am Freitag, 20. März 2015 14:31:34 UTC+1 schrieb Filipe Ximenes:
Is making two serializers an option?
There could be an mixin with the shared data and each one (the create one and the get one) could implement its customisations.
On Fri, Mar 20, 2015 at 10:23 AM, Sven Mäurer <maeure...@gmail.com> wrote:
I want to make a serializer which serializes a related object on get but for creation I just need the id. It sounds very simple for me and may I have a blocker in my brain.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ion Scerbatiuc

unread,
Mar 21, 2015, 10:43:41 AM3/21/15
to django-res...@googlegroups.com
Sven,

I think your solution looks good and on the first look I think I would use the same approach. Shouldn't be too hard to tweak `to_internal_value` to properly handle your use case.

@Filipe's solution sounds good to:
1. Implement two separate serializers: one read-only and one write-only
2. Implement a custom view to use the write-only on create/update and the other one for presenting results

I like the single serializer solution best, because you can use it easily with a generic view, without needing to build custom view logic to handle deserialization/serialization.

-Ion

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

Sven Mäurer

unread,
Mar 21, 2015, 10:56:19 AM3/21/15
to django-res...@googlegroups.com
At least I got it working with the two serializer where I set the one with just the id in the viewsets create method.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

JR Nielsen

unread,
May 6, 2015, 12:02:35 PM5/6/15
to django-res...@googlegroups.com
Hi Sven, can you explain how you got this working? I've tried implementing this in my project and it causes problems. It actually works, but django throws an error that I don't understand when I try to update records (even though the record is in fact updated). This is what my code looks like:

class ComplianceCodeSerializer(serializers.ModelSerializer):
   
class Meta:
        model
= ComplianceCode
        fields
= (...)

class ComplianceSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    compliance_code
= ComplianceCodeSerializer(read_only=True)
    compliance_code_id
= serializers.PrimaryKeyRelatedField(queryset=ComplianceCode.objects.all(), write_only=True)
   
class Meta:
        model
= Compliance
        fields
= (...)

And when I try to update a record via the browsable api, I get the following type error:

int() argument must be a string or a number, not 'ComplianceCode'

Maybe there is something else wrong with my code? but removing the id field fixes the error. In fact, removing either one of the fields fixes the error, it just doesn't like having both of them set for some reason.

Andrew Pashkin

unread,
May 11, 2016, 10:35:12 AM5/11/16
to Django REST framework
On Wednesday, May 6, 2015 at 7:02:35 PM UTC+3, JR Nielsen wrote:
Hi Sven, can you explain how you got this working? I've tried implementing this in my project and it causes problems. It actually works, but django throws an error that I don't understand when I try to update records (even though the record is in fact updated). This is what my code looks like:

class ComplianceCodeSerializer(serializers.ModelSerializer):
   
class Meta:
        model
= ComplianceCode
        fields
= (...)

class ComplianceSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    compliance_code
= ComplianceCodeSerializer(read_only=True)
    compliance_code_id
= serializers.PrimaryKeyRelatedField(queryset=ComplianceCode.objects.all(), write_only=True)
   
class Meta:
        model
= Compliance
        fields
= (...)

And when I try to update a record via the browsable api, I get the following type error:

int() argument must be a string or a number, not 'ComplianceCode'

Maybe there is something else wrong with my code? but removing the id field fixes the error. In fact, removing either one of the fields fixes the error, it just doesn't like having both of them set for some reason.
 
I bet it happens because compliance_code_id is the actual model field which stores id in the related table and it is integer. 
Reply all
Reply to author
Forward
0 new messages