These are chat archives for django/django

30th
Nov 2018
Tanveer Ahmed
@tanveerobjects_twitter
Nov 30 2018 04:09
Hi Everyone, I need help :(
How can I change Add to Basket Button to Buy Now in Django Oscar?
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 13:38
Hi
how to solve this object has no attribute 'get_serializer'?
i used djangorestframework
fsecada01
@fsecada01
Nov 30 2018 13:42
what does the full error message sa
say
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 13:43
Screen Shot 2018-11-30 at 20.42.40.png
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 13:47
I already read, but i still confuse. I am first in python. Maybe my views.py can help you to help me
class CustomerList(mixins.CreateModelMixin, mixins.UpdateModelMixin, APIView):
    def get(self, request):
        customerlist = Customer.objects.all()
        serializer = CustomerSerializer(customerlist, many=True)
        return Response(serializer.data)

    def post(self, request, *args, **kwargs):
        customerlist = Customer.objects.all()
        serializer = CustomerSerializer(customerlist, many=True)
        self.create(request, *args, **kwargs)
        return Response(serializer.data)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)
with this code i can GET, but when i POST the error show
fsecada01
@fsecada01
Nov 30 2018 13:49
you're missing GenericAPIView
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 13:51
where should i put it?
fsecada01
@fsecada01
Nov 30 2018 13:51
i imagine instead of APIView
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 13:54

class GenericAPIView(APIView):
    """
    Base class for all other generic views.
    """

    def get_serializer(self, *args, **kwargs):
        """
        Return the serializer instance that should be used for validating and
        deserializing input, and for serializing output.
        """
        serializer_class = self.get_serializer_class()
        kwargs['context'] = self.get_serializer_context()
        return serializer_class(*args, **kwargs)

class CustomerList(mixins.CreateModelMixin, mixins.UpdateModelMixin, GenericAPIView):
    def get(self, request):
        customerlist = Customer.objects.all()
        serializer = CustomerSerializer(customerlist, many=True)
        return Response(serializer.data)

    def post(self, request, *args, **kwargs):
        customerlist = Customer.objects.all()
        serializer = CustomerSerializer(customerlist, many=True)
        self.create(request, *args, **kwargs)
        return Response(serializer.data)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)
I already implemented this, but still got error with difference message 'CustomerList' object has no attribute 'get_serializer_class'
is there any wrong?
fsecada01
@fsecada01
Nov 30 2018 13:57
i think you should identify your model and qs on the class level to keep things dry. So maybe rebase some of your code to be as follows:
class CustomerList([insert mixins and inherited views]):
    model = Customer
    qs = model.objects.all()
    serializer = CustomerSerializer(qs, many=True)

    def foo(fooness): 
        etc

See how CustomerSerializer is being defined and problemshoot from there, likely from shell.

You may be advised to download django_extensions and install jupyter notebook so that you can run python manage.py shell_plus --notebook in the teriminal to play with your code in Jupyter

tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 14:09
i did it, but still got same error
class CustomerList(mixins.CreateModelMixin, mixins.UpdateModelMixin, GenericAPIView):
    model = Customer
    qs = model.objects.all()
    serializer = CustomerSerializer(qs, many=True)

    def get(self, request):
        customerlist = Customer.objects.all()
        serializer = CustomerSerializer(customerlist, many=True)
        return Response(serializer.data)
Tanveer Ahmed
@tanveerobjects_twitter
Nov 30 2018 14:10
Hi everyone!
How can I replace Add to Basket into Buy now in Django Oscar?
fsecada01
@fsecada01
Nov 30 2018 14:10
you do not need to define customerlist or serializer in your get method now, because you've defined it on the class-level.
class CustomerList(mixins.CreateModelMixin, mixins.UpdateModelMixin, GenericAPIView):
    model = Customer
    qs = model.objects.all()
    serializer = CustomerSerializer(qs, many=True)

    def get(self, request):
        return Response(self.serializer.data)
Although something to keep in mind is that your get function does not seem to be doing anything with the request argument
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 14:16
now i got problem in
class GenericAPIView(APIView):
    """
    Base class for all other generic views.
    """

    def get_serializer(self, *args, **kwargs):
        """
        Return the serializer instance that should be used for validating and
        deserializing input, and for serializing output.
        """
        serializer_class = self.get_serializer_class()
        kwargs['context'] = self.get_serializer_context()
        return serializer_class(*args, **kwargs)
here the error 'CustomerList' object has no attribute 'get_serializer_class'
fsecada01
@fsecada01
Nov 30 2018 14:43
how is CustomerSerializer defined
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 14:44
class CustomerSerializer(serializers.ModelSerializer):

    class Meta:
        model = Customer
        fields = '__all__'
fsecada01
@fsecada01
Nov 30 2018 14:53
Try this:
class CustomerList(mixins.CreateModelMixin, mixins.UpdateModelMixin, GenericAPIView):

    model = Customer
    qs = model.objects.all()
    serializer = CustomerSerializer(qs, many=True)

    def get(self, request):
        return Response(self.serializer.data)

    def post(self, request, *args, **kwargs):
        self.create(request, *args, **kwargs)
        return Response(self.serializer.data)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 15:06
still got this error 'CustomerList' object has no attribute 'get_serializer_class', is there any problem with version of Python or Django?
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 16:19
i try to make like this and it's works
class CustomerList(CreateAPIView, ListAPIView):

    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
thank you all for help
fsecada01
@fsecada01
Nov 30 2018 16:32
woot!
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 16:44
How about this error message? Expected view TransactionList to be called with a URL keyword argument named "pk". Fix your URL conf, or set the.lookup_fieldattribute on the view correctly. i got when i used UpdateAPIView
fsecada01
@fsecada01
Nov 30 2018 16:46
pk_url_kwarg = 'foo_id'

def get_object(self, querset=None):
    return model.object.get(pk=self.foo.id)
you need to define the get_object function unless you want to use the default get_object function as inherited from the UpdateView. Just make sure that you define pk_url_kwarg so that the view knows how to look up the object.
tandrysyawaludin
@tandrysyawaludin
Nov 30 2018 16:51
where should i put it?