1

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

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?

2 Answers 2

1

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__"
Sign up to request clarification or add additional context in comments.

Comments

0

Your solution should work with one small modification:

models.CharField(validators=[int_list_validator], max_length=100)

1 Comment

what is int_list_validator here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.