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.
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.
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.
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.
--
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.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
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 = (...)
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.
compliance_code_id is the actual model field which stores id in the related table and it is integer.