title: How to create an object and then set related field in Django Rest Framework
body: <p>I'm trying to create a new object using Django REST Framework's <code>ModelViewSet</code> and the <code>ModelSerializer</code> and associate an existing object to it's <code>ForeignKey</code> field. I have two models with a one-to-many relationship, basically a manufacturer and a product. The manufacturer already exists in the database and I'm trying to add a product individually and assign it's manufacturer at the same time. When I do, DRF is giving me this error:</p>
  
  <blockquote>
    <p>"non_field_errors": [
        "Invalid data. Expected a dictionary, but got Product."
    ]</p>
  </blockquote>
  
  <p>Here are what my models look like (I've tried to take out the unneeded code to keep it small). When a product is created the <code>manufacturer</code> field is an integer representing the ID of it's related object. As you can see in my view, I query for the <code>Manufacturer</code> object, set it and then continue to let DRF create the object. If someone can please tell me where I'm going wrong, I'd greatly appreciate it!</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Manufacturer(models.Model):
      name = models.CharField(max_length=64)
      state = models.CharField(max_length=2)
  
  class Product(models.Model):
      manufacturer = models.ForeignKey(Manufacturer, related_name="products")
      name = models.CharField(max_length=64)
      sku = models.CharField(max_length=12)
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class ProductViewSet(view sets.ModelViewSet):
      model = Product
      permission_classes = [IsAuthenticated]
  
      def get_queryset(self):
          # get logged in user's manufacturer
          return Product.objects.filter(manufacturer=manufacturer)
  
      def create(self, request, *args, **kwargs):
          if "manufacturer" in request.data:
              manufacturer = Manufacturer.objects.get(pk=request.data["manufacturer"])
              request.data["manufacturer"] = manufacturer
  
          return super(ProductViewSet, self).create(request, *args, **kwargs)
  </code></pre>
  
  <p><strong>serializers.py</strong></p>
  
  <pre><code>class ManufacturerSerializer(serializers.ModelSerializer):
      class Meta:
          model = Manufacturer
          fields = ("id", "name", "state",)
          read_only_fields = ("id",)
  
  class ProductSerializer(serializers.ModelSerializer):
      manufacturer = ManufacturerSerializer()
  
      class Meta:
          model = Product
          fields = ("id", "manufacturer", "name",)
          read_only_fields = ("id",)
  </code></pre>