Rest API Django does not work

Asked

Viewed 149 times

0

I have a problem in Python/Django. The tables school and school_application_info are related. However, there is no record of any school_id in school_application_info. So I added null=True, blank=True staying that way:

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.']}
  • which library you are using for the Rest api??

  • I am using Django-Rest-framework.

  • 1

    Lucas, could you share the view code that gets the call?

1 answer

2

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

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

    class Meta:
         model = School

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

See in the documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.