# Posting empty foreign key field when creating parent object

**URL:** <https://forum.djangoproject.com/t/posting-empty-foreign-key-field-when-creating-parent-object/9852>\
**Category:** Using Django\
**Created:** [September 27, 2021, 12:20am UTC](https://forum.djangoproject.com/t/posting-empty-foreign-key-field-when-creating-parent-object/9852 "2021-09-27T00:20:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zigzagdoom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zigzagdoom/32/5913_2.png) [@zigzagdoom](https://forum.djangoproject.com/u/zigzagdoom)\
**Post date:** [September 27, 2021, 12:20am UTC](https://forum.djangoproject.com/t/posting-empty-foreign-key-field-when-creating-parent-object/9852/1 "2021-09-27T00:20:45Z")

</div>

Hi,

I have the following models (please not these are simplified from a much larger model structure where this relationship schema does indeed make sense):

My models.py:

```auto
class Supplier(models.Model):
	name = models.CharField(max_length=500)
	address = models.CharField(max_length=500, blank=True)

class NationalIndustry(models.Model):
	name = models.CharField(max_length=500)
	code = models.CharField(max_length=500)

	def __str__ (self):
		return f"{self.name} - {self.code}"

# This maps an industry to a supplier.
class SupplierIndustry(models.Model):
	supplier = models.ForeignKey(Supplier, related_name='industries', on_delete=models.CASCADE, blank=True)
	industry = models.ForeignKey(NationalIndustry, related_name='suppliers', on_delete=models.SET_NULL, null=True, blank=True)
	code = models.CharField(max_length=500)

```

My serializers.py:

```auto
class SupplierIndustrySerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierIndustry
        fields = ['industry', 'code']

class SupplierSerializer(serializers.ModelSerializer):
    industries = SupplierIndustrySerializer(many=True)

    class Meta:
        model = Supplier
        fields = ['id', 'name', 'address', 'contact_number', 'description', 'industries']

```

My supplier view (because I have a large dataset, I want to be able to post lists at a time):

```auto
class SupplierList(generics.ListCreateAPIView):
    queryset = Supplier.objects.all()
    serializer_class = SupplierSerializer
    pagination_class = SmallResultsSetPagination
    
    def create(self, request, *args, **kwargs):
        try:
            serializer = self.get_serializer(data=request.data, many=True)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
        except Exception as e:
            print(e)
            return Response({}, status=status.HTTP_400_BAD_REQUEST)

```

Now the issue is, when I post to the supplier endpoint, I get the following issue:

> {‘industries’: [ErrorDetail(string=‘This field is required.’, code=‘required’)

If I remove the `industries` field from the `SupplierSerializer` it all works fine, but if I include it as per the code above, it gives the error.

Can someone please explain how I can make the `industries` array such that it can be empty? Any guidance would be much appreciated!

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 28, 2021, 3:21pm UTC](https://forum.djangoproject.com/t/posting-empty-foreign-key-field-when-creating-parent-object/9852/2 "2021-09-28T15:21:11Z")

</div>

I believe you’re running up against a common issue with DRF. Check out the docs on [writable nested serializers](https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers).
