Rest API Django does not work

0

I have a problem with Python/Django . The school and school_application_info tables are related. However, there is no record with some school_id in school_application_info . So I added null=True, blank=True by going like this:

school = models.OneToOneField(School, related_name='school_application_info', null=True, blank=True)

Then I did the Django migrations commands:

./manage.py makemigrations
./manage.py migrate 

But it is not working and generates an error:

SchoolViewSet: ErrorResponse - status:400, resp:{'school_application_info': [u'This field may not be null.']}
    
asked by anonymous 17.05.2016 / 22:22

1 answer

1

PrimaryKeyRelatedField can be used to represent the relation using its primary key:

class SchoolSerializer(serializers.ModelSerializer):
    school_application_info = serializers.PrimaryKeyRelatedField(allow_null=True)

    class Meta:
         model = School

The argument allow_null if set to True , the field will accept Null values or empty strings for null relationships. The default is False .

See the documentation

    
04.07.2016 / 22:37