Foreign Key does not allow null values

Submitted 3 years, 6 months ago
Ticket #241
Views 293
Language/Framework Django
Priority Medium
Status Closed

using the Django REST Framework 2.0.

Here is my model class:

class Mission(models.Model):
  assigned_to = models.ForeignKey('auth.User',
                                   related_name='missions_assigned',
                                   blank = True)

Here is my view class:

class MissionList(generics.ListCreateAPIView):
    model = Mission
    serialize_class = MissionSerializer
  1. The multipart form is rendered in the browser with empty choice for assigned_to field.

  2. When posting raw JSON, I get the following error message:

Cannot assign None: "Mission.assigned_to" does not allow null values.

Submitted on Oct 19, 20
add a comment

1 Answer

Verified

It is showing error because you need to add null=True in your field in order to send null value in the database.

The blank option is used in the form validation, and the null is used when writing to database.

So you might add null=True to that field.

Submitted 3 years, 6 months ago


Latest Blogs