Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add array of integer field in Django Rest Framework?

I want to add an array of integer fields in my model

class Schedule(models.Model):
    name = models.CharField(max_length=100)
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    day_of_the_week = ?? ( array of integer )

I tried with

class Schedule(models.Model):
    name = models.CharField(max_length=100)
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    day_of_the_week = models.CharField(max_length=100)

and in the serializer add ListField

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ScheduleSerializer(serializers.ModelSerializer):
    day_of_the_week = serializers.ListField()

    class Meta():
        model = Schedule
        fields = "__all__"

but this one is not working can anyone suggest me how to deal with this issue?

>Solution :

Try this:

class Schedule(models.Model):
    name = models.CharField(max_length=100)
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    day_of_the_week = models.JSONField(default=list)

class ScheduleSerializer(serializers.ModelSerializer):
    day_of_the_week = serializers.ListField(
        child=serializers.IntegerField(),
    )

    class Meta():
        model = Schedule
        fields = "__all__"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading