tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.4k stars 266 forks source link

i want to create listserializer im getting this error #1062

Closed anky808 closed 1 year ago

anky808 commented 1 year ago
 File "/home/vagrant/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/vagrant/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 204, in _get_response
    response = response.render()
  File "/home/vagrant/env/lib/python3.6/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/home/vagrant/env/lib/python3.6/site-packages/rest_framework/response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/home/vagrant/env/lib/python3.6/site-packages/rest_framework/renderers.py", line 723, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "/home/vagrant/env/lib/python3.6/site-packages/rest_framework/renderers.py", line 654, in get_context
    raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)
  File "/home/vagrant/env/lib/python3.6/site-packages/rest_framework/renderers.py", line 563, in get_raw_data_form
    for name, field in serializer.fields.items():
AttributeError: 'DynamicListSerializer' object has no attribute 'fields'

models.py

class tbl_machinery_running_hour(models.Model):
    """ Create machinery running hour table in database """
    id = models.AutoField(
        primary_key=True
    )
    machinery_name = models.ManyToManyField(
        tbl_machinery_list_master
    )
    running_hours_since_last_report = models.TimeField()
    avg_power = models.IntegerField(
        validators=[
            RegexValidator('^[0-9]{1,2}$')
        ],
    )
    vessel_name = models.ForeignKey(
        tbl_vessel_general_information,
        on_delete=models.DO_NOTHING,
        default=""
    )
    voyage_information = models.ForeignKey(
        tbl_voyage_information,
        on_delete=models.DO_NOTHING,
        default=""
    )
    vessel_reporting_information = models.ForeignKey(
        tbl_vessel_reporting_information,
        on_delete=models.DO_NOTHING,
        default=""
    )
    created_on = models.DateTimeField(
       auto_now_add=True,
    )
    modified_on = models.DateTimeField(
        auto_now=True
    )
    created_by = models.ForeignKey(
        UserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name='%(class)s_created'
    )
    modified_by = models.ForeignKey(
        UserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name='%(class)s_modified'
    )
    display = models.BooleanField(
        default=True
    )
serializers.py
class MachineryRunningHourSerializer(serializers.ModelSerializer):
    """ serializer for machinery running hours """

    class Meta:
        model = tbl_machinery_running_hour
        fields = "__all__"
        read_only_fields = ['created_by', 'modified_by','display']

class DynamicListSerializer(serializers.ListSerializer):
    child = MachineryRunningHourSerializer()

    def to_representation(self, data):
         return data

    def create(self, validated_data):
         machinery  = [tbl_machinery_running_hour(**item) for item in validated_data]
         return tbl_machinery_running_hour.objects.bulk_create(machinery)
views.py
class MachineryRunningHourViewSet(viewsets.ModelViewSet):
    """ View for manage machinary running hours """
    queryset = models.tbl_machinery_running_hour.objects.all()
    serializer_class = MachineryRunningHourSerializer()
    authentication_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]

    def get_serializer_class(self):
        if self.action =='create':
             return DynamicListSerializer
        return MachineryRunningHourSerializer
anky808 commented 1 year ago

plz provide the correct code

tfranzel commented 1 year ago

I'm pretty sure the error comes from DRF and not spectacular, so this is not our issue I suppose.

I think you a are using ListSerializer wrong. serializers.ListSerializer(child=XXXSerializer()) is how it is supposed to be used. I guess ModelViewSet cannot handle the ListSerializer class by default. You may need to write a custom create method. and then use @extend_schema appropriately.

anky808 commented 1 year ago

thankyou