How Do I Include ForeignKey in django-rest-framework POST
<p>So I have tried to make a browsable API via <code>django-rest-framework (DRF)</code>, but I have had some issues nesting serializers. So far, I am able to include the <code>Sport</code> and <code>Category</code> fields/foreignkeys into my <code>Article</code>, but when I try to <code>POST</code> via the API, I get an error saying as follows:</p>

<blockquote>
  <p>Got a <code>TypeError</code> when calling <code>Article.objects.create()</code>. This may be because you have a writable field on the serializer class that is not a valid argument to <code>Article.objects.create()</code>. You may need to make the field read-only, or override the ArticleSerializer.create() method to handle this correctly.
  Original exception text was: int() argument must be a string, a bytes-like object or a number, not 'ArticleSport'.</p>
</blockquote>

<p>Here are my files:</p>

<p><strong>models.py</strong></p>

<pre><code>[...]

class ArticleSport(TimeStampedModel):
    title = models.CharField(max_length=20, blank=False)
    slug = AutoSlugField(populate_from='title', unique=True, always_update=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')  # TODO: Add on_delete?

    uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)

    def __str__(self):
        return '{0}'.format(self.title)

    #class Meta:  # TODO: Migrate live
        #unique_together = ('title', 'parent')


class ArticleCategory(TimeStampedModel):
    title = models.CharField(max_length=20, blank=False)
    slug = AutoSlugField(populate_from='title', unique=True, always_update=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')  # TODO: Add on_delete?

    uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)

    def __str__(self):
        return '{0}'.format(self.title)

    class Meta:
        verbose_name_plural = 'article categories'
        #unique_together = ('title', 'parent')  # TODO: Migrate live


class Article(TimeStampedModel):
    DEFAULT_FEATURED_IMAGE = settings.STATIC_URL + 'images/defaults/default-featured-image.png'

    title = models.CharField(max_length=160, blank=False)
    slug = AutoSlugField(populate_from='title', unique=True, always_update=True)
    sport = models.ForeignKey(ArticleSport, on_delete=models.CASCADE, related_name='articleAsArticleSport')
    category = models.ForeignKey(ArticleCategory, on_delete=models.CASCADE, related_name='articleAsArticleCategory')
    featured_image = models.ImageField(upload_to=PathAndUniqueFilename('featured-images/'), blank=True)
    featured_image_caption = models.CharField(max_length=100, blank=True)
    views = models.IntegerField(default=0)

    uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)

    def get_absolute_url(self):
        return reverse('main:article_specific', args=[self.slug])  # TODO: Remove if standalone pages are removed

    def get_featured_image(self):
        if self.featured_image:
            return self.featured_image.url
        else:
            return self.DEFAULT_FEATURED_IMAGE

    def get_comment_count(self):
        return ArticleComment.objects.filter(article=self).count()

    def __str__(self):
        return '{0}'.format(self.title)

[...]
</code></pre>

<p><strong>urls.py</strong></p>

<pre><code>[...]

class ArticleSportSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ArticleSport
        fields = ('id', 'title', 'parent', 'created', 'modified')


class ArticleCategorySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ArticleCategory
        fields = ('id', 'title', 'parent', 'created', 'modified')


class ArticleSerializer(serializers.HyperlinkedModelSerializer):
    sport = ArticleSportSerializer(read_only=True)
    sport_id = serializers.PrimaryKeyRelatedField(queryset=ArticleSport.objects.all(), write_only=True)
    category = ArticleCategorySerializer(read_only=True)
    category_id = serializers.PrimaryKeyRelatedField(queryset=ArticleCategory.objects.all(), write_only=True)
    modified = serializers.HiddenField(default=timezone.now())  #TODO: Figure out how to implement this

    class Meta:
        model = Article
        fields = ('id', 'title', 'sport', 'sport_id', 'category', 'category_id', 'featured_image', 'featured_image_caption', 'views', 'created', 'modified')

[...]
</code></pre>

<p>Sample POST to API:</p>

<pre><code>{
    "title": "This is a test Title",
    "sport_id": 1,
    "category_id": 1,
    "featured_image": null,
    "featured_image_caption": "",
    "views": null,
    "modified": null
}
</code></pre>