Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

python - Django rest framework, do serializer require fields to be specified?

Let's say we have model:

class CompanySource(models.Model):
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company_source')
    source = models.CharField(max_length=1500)

and we want to have seralizer for it and we use HyperlinkedModelSerializer it looks like this

class CompanySourceSerializer(serializers.HyperlinkedModelSerializer):
    def create(self, validated_data):
        return CompanySource.objects.create(**validated_data)

    class Meta:
        model = CompanySource
        fields = ['id', 'company', 'source']

is it required to set source = serializers.CharField(max_length=1500) inside Serliazer and what does it contribute to? Thank you

question from:https://stackoverflow.com/questions/65937772/django-rest-framework-do-serializer-require-fields-to-be-specified

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is not important.

Rest framework serializers are designed to take the hassle out of your hands.

Refer to here.

And for your case, why not use Model serializer?. You do not seem to be needing links


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...