Home > front end >  Django-restframework - use the same value on differents fields
Django-restframework - use the same value on differents fields

Time:01-19

I'm building an API using Django-restframework.
models.py

class Researches(models.Model):
    research_id = models.BigAutoField(primary_key=True)

I would like to use the same value on two differents fileds, like:

[
    {
        "research_id": 1,
        "id": 1
    },
]

Is it possible?

CodePudding user response:

You need to do something like this:

class ResearchesSerializer(
    serializers.ModelSerializer
):
    id = serializers.IntegerField(
        source="research_id", read_only=True
    )
    class Meta:
        model = Researches
        fields = (
            "research_id",
            "id",

        )

for ref: https://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly

  • Related