Lets say I want to create an article that links via slug to one or multiple other articles (for now one). A post request comes in giving me some article-info and the related slug. I want to
models.py
class Article(models.Model):
heading = models.CharField(max_length=2550)
slug = models.SlugField(null=True, default=None, unique=True, max_length=255)
article_previous = models.ForeignKey('self', to_field='slug', blank=True, null=True,
related_name='subcategories', on_delete=models.DO_NOTHING)
serializers.py
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = (
"heading",
"slug",
"article_previous",
def create(self, validated_data):
try:
article_previous = Article.objects.get(slug=d['value'])
article_previous_slug = article_previous.slug
except Article.DoesNotExist:
raise serializers.ValidationError(f'No Article with the slug {d["value"]}')
article=Article.objects.create(**validated_data,article_previous_id=article_previous_slug)
return validated_data
article_previous = serializers.CharField()
ValueError: Cannot assign "'test_1'": "Article.article_previous" must be a "Article" instance.
article_previous = serializers.CharField('article.article_previous')
TypeError: Article() got an unexpected keyword argument 'article'
views.py
:return Response(serializer.validated_data, status=status.HTTP_201_CREATED, headers=headers)
response = serializer.save()
return Response(response, status=status.HTTP_201_CREATED, headers=headers)
TypeError: Object of type Article is not JSON serializable.
You can serialize the slug by working with the article_previous_id
field:
class ArticleSerializer(serializers.ModelSerializer):
article_previous = serializers.CharField(source='article_previous_id')
class Meta:
model = Article
fields = ('heading', 'slug', 'article_previous')
# …
class ArticleSerializer(serializers.ModelSerializer):
article_previous=serializers.SlugRelatedField(queryset=Article.objects.all(), slug_field="slug")
class Meta:
model = Article
fields = ('heading', 'slug', 'article_previous')
def create(self, validated_data):
article_previous=validated_data.pop("article_previous")
article=Article.objects.create(**validated_data, article_previous=article_previous)
validated_data.update( {"article_previous" : article.article_previous.slug} )
return validated_data
I think this solution has the advantage that the naming stays article_previous
and is not changed to article_previous_id
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.