These are chat archives for django/django

16th
Sep 2018
Sanan R. Fataliyev
@sanan-fataliyev
Sep 16 2018 00:13
class User(models.Model):
    user_id = models.ForeignKey(Login, primary_key=True)
    #...
Yash Kedia
@yash-kedia
Sep 16 2018 04:57
This message was deleted
MilanRgm
@MilanRgm
Sep 16 2018 09:19
what is this error means?
'dict' object has no attribute '_meta'
Miguel
@miguel550
Sep 16 2018 09:20
@MilanRgm please show the entire traceback
MilanRgm
@MilanRgm
Sep 16 2018 09:24

class CompanySerializer(serializers.ModelSerializer):

    class Meta:

        model = Company
        fields = '__all__'
        read_only_fields = ('owner', )


class CompanyAPIView(views.APIView):

    read_serializer_class = CompanyReadSerializer
    serializer_class = CompanySerializer


    def post(self, request, pk=None, format=None):
        reply = {}
        company={}
        if pk is not None:
            try:
                company = Company.object.get(pk=pk)
            except Company.DoesNotExist:
                return error.RequestedResourceNotFound().as_response()
        serialized_data = self.serializer_class(instance=company, data=request.data, partial=True)
        if serialized_data.is_valid():
            print('#######################valid#####################')
            print('request.user', request.user)
            serialized_data.save(owner=request.user)
        else:
            return error.ValidationError(serialized_data.errors).as_response()
        print("##################################")
        print("serialized_data", serialized_data)
        reply['data'] = serialized_data.data
        return Response(reply, status.HTTP_200_OK)
This is my code the request.user also gets printed. I tried from admin and it works but not from api
@miguel550 Exception Type: AttributeError. It says and exception location is shown here Exception Location: /home/milan/.virtualenvs/franchise/lib/python3.6/site-packages/rest_framework/utils/model_meta.py in get_field_info, line 35. Do you mean to say this by trace?
Miguel
@miguel550
Sep 16 2018 09:29
Then I'm assuming the error happens when serialized_data.save(owner=request.user)
is executed
MilanRgm
@MilanRgm
Sep 16 2018 09:30
yes
issue is there
i was not sure before but issue is there only the below line is not printed. I mean "serialized_data" line
Miguel
@miguel550
Sep 16 2018 09:31
The error that I see here is that at first you are assigning an empty dictionary to company, and if the pk is None then it stay as a dictionary and get passed as an argument to serializer_class
MilanRgm
@MilanRgm
Sep 16 2018 09:31
I have created a custom user model by inheriting the AbstractUser.
Miguel
@miguel550
Sep 16 2018 09:32
you should make sure that an instance of company is passed to the serializer_class method
MilanRgm
@MilanRgm
Sep 16 2018 09:32
That might be the wrong way but my intention was if there is pk, update the company object otherwise create a new one.
Miguel
@miguel550
Sep 16 2018 09:33
then instead do company = Company()
MilanRgm
@MilanRgm
Sep 16 2018 09:33
so it is wrong to pass empty dict as instance when creating a new one?
Miguel
@miguel550
Sep 16 2018 09:34
It seems that serializer_class is expecting an instance of company instead of a dict
MilanRgm
@MilanRgm
Sep 16 2018 09:36
Instance of company instead of empty dict worked but when i used the pk, the company form is not filled with data.
Thanks @miguel550 for your help.