I am using Django REST generic views for my API endpoint. One of the field in my serializer has ManyToMany relationship. I want to show that field into my API endpoint, But getting this error
Lists are not currently supported in HTML input.
My view is this:
class AlertCreateView(ListCreateAPIView):
permission_classes = (IsAuthenticated,)
pagination_class = None
serializer_class = AlertSerializer
def get_queryset(self):
queues = Queue.objects.all()
for queue in queues:
queryset = Alert.objects.filter(
queue=queue
)
return queryset
My Serializer is this:
class AlertSerializer(serializers.ModelSerializer):
queue = QueueSerializer(many=True)
class Meta:
model = Alert
fields = (
'id', 'name', 'queue','email', 'expected_qos'
)
You do not need the get_queryset
method you could do something like this:
#views.py
class AlertCreateView(ListCreateAPIView):
queryset = Alert.objects.all()
serializer_class = AlertSerializer
permission_classes = (IsAuthenticated,)
Name the queues
field in the serializer in the same way as it is written in therelated_name
of the model. And your QueueSerializer
can inherit fromPrimaryKeyRelatedField
to be rendered.
#models.py
class AlertModel(models.Model):
...
queues = models.ManyToManyField(Queue, ... related_name='queues')
...
#serializer.py
class QueueSerializer(PrimaryKeyRelatedField, serializers.ModelSerializer):
class Meta:
model: Queue
class AlertSerializer(serializers.ModelSerializer):
queues = QueueSerializer(many=True, queryset=Queue.objects.all())
class Meta:
model = Alert
fields = (
'id', 'name', 'queues','email', 'expected_qos'
)