Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use drf_turbo serializer #9

Open
hossamdebyene opened this issue May 24, 2022 · 15 comments
Open

How to use drf_turbo serializer #9

hossamdebyene opened this issue May 24, 2022 · 15 comments

Comments

@hossamdebyene
Copy link

  • drf-turbo version:0.1.5
  • Python version:3.9
  • Operating System:win10

Description

I am trying to save data in the postgres using django with the serializer implemented in cython

What I Did

I followed the process you written in the documentations but the cython build i didn't know how to build and if i use dt.serializer it gives me in save no object has no attribute 'create' and if i use dr.ModelSerializer

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
@Mng-dev-ai
Copy link
Owner

Can you show me your code please?

@hossamdebyene
Copy link
Author

Screenshot (20)_LI
Screenshot (18)

These are the serializers with the code i highlighted under it

@Mng-dev-ai
Copy link
Owner

I can't see any errors in the console, all I see is the instance of the serializer only.
Can you please share how did you use the serializer within the view and what errors you got?

@HDebiane
Copy link

Did you checked the both pictures I attached with the issue
i used the serializer to save data in the loop the problem is when using the serializer.save() it gives the highlighted issue in the first picture in case you can send me your email or phone number to contact you

@Mng-dev-ai
Copy link
Owner

I can't see where do you use the serializer, please update your code with a clear example.

@DarViSte
Copy link

DarViSte commented Sep 20, 2022

Hello,
Looks like I have similar error.
drf-turbo version:0.1.5
Python 3.6.9
Django version 3.2.15

Description
I call serializer from apiviews.py and the error is

Traceback (most recent call last):
  File "drf_turbo/fields.pyx", line 214, in drf_turbo.fields.Field.get_attribute
  File "drf_turbo/utils.pyx", line 57, in drf_turbo.utils.get_attribute
  File "drf_turbo/utils.pyx", line 64, in drf_turbo.utils.get_attribute
AttributeError: 'xxx' object has no attribute 'xxx_atr'

The error appears when nested serializer called.
I cannot share my code but will do my best to answer your questions.

@Mng-dev-ai
Copy link
Owner

Hello @DarViSte, Can you share any examples that I can test?

@DarViSte
Copy link

@Mng-dev-ai here is what I have:
model.py

class XXX(models.Model):
    xxx_id = models.AutoField(primary_key=True)
    event_id = models.IntegerField(blank=True, null=True)
    name = models.IntegerField()
    xxx_year = models.SmallIntegerField()
    
    class Meta:
        managed = False
        db_table = 'xxx'
        unique_together = ('name', 'xxx_year')

serializer.py

class userXXXSerializer(dt.Serializer):
    xxx_id = dt.IntField(read_only=True)
    name = dt.IntField(read_only=True)
    xxx_year = dt.IntField(read_only=True)

class XXXSerializer(dt.Serializer):
   info = userXXXSerializer(read_only=True)
   
   def to_representation(self, instance):
       xxx = instance
       list = {
                 'info' : userXXXSerializer(xxx).data,
                  ''errors: []
                }
        return list

apiviews.py

serializer = XXXSerializer(queryset, many=True)
data = serializer.data

Error is:

Traceback (most recent call last):
  File "drf_turbo/fields.pyx", line 214, in drf_turbo.fields.Field.get_attribute
  File "drf_turbo/utils.pyx", line 57, in drf_turbo.utils.get_attribute
  File "drf_turbo/utils.pyx", line 64, in drf_turbo.utils.get_attribute
AttributeError: 'XXX' object has no attribute 'info'

This structure looks very similar to what I have in my code

@Mng-dev-ai
Copy link
Owner

Thank you @DarViSte
I should have mentioned that in the docs, I renamed to_representation > serialize and to_internal_value > deserialize
so it should be:

def serialize(self, instance, context):
     ...

btw, you must add context argument to the method

@DarViSte
Copy link

Hello @Mng-dev-ai
Thank you very much for your help. The error is solved now.
Just one question. When I call regular serializer serialization runs one by one, but when I call drf-turbo it gets whole queryset at once. Is it how it work or I do something wrong.

P.S. I put the context as parameter just forgot to write it :)

@Mng-dev-ai
Copy link
Owner

Mng-dev-ai commented Sep 21, 2022 via email

@DarViSte
Copy link

@Mng-dev-ai I'll try to explain. I have now two serializers, old one and drf-turbo, when I run query first with one and then with another I have a different result:
Serializer:

class XXXSerializer(serializer.Serializer):
   info = userXXXSerializer(read_only=True)
   
   def to_representation(self, instance, context):
       xxx = instance
       print (xxx)

it prints out:

Pick object (1)
Pick object (2)
Pick object (3)
...

When I call drf-turbo serializer, result for print(xxx) is:

<QuerySet [<Pick: Pick object (1)>, <Pick: Pick object (2)>, <Pick: Pick object (3)>...']>

I have a feeling that it is some small bug in my script which I do not see

@DarViSte
Copy link

Hello @Mng-dev-ai Thank you very much for your help. Looks like that the question above is still on the surface :) Is it so that drf-turbo works with single objects, not sets?
Like in this example:

serializer = UserSerializer(user)
serializer.data

user is just one object not a set of the same objects.

@Mng-dev-ai
Copy link
Owner

Mng-dev-ai commented Sep 23, 2022

The way drf-turbo working is a little bit different than rest_framework so as you said when you print(xxx) the result is:
<QuerySet [<Pick: Pick object (1)>, <Pick: Pick object (2)>, <Pick: Pick object (3)>...']>
So for your example, you need to do something like that:

 def serialize(self, instance, context):
    if self.many:
        for o in instance:
            # add your logic here

Conclusion: If you need to override serialize method then you need to add some extra code than what you used to do in rest_framework otherwise it should work as usual like the below example:

class UserSerializer(dt.Serializer):
    email = dt.EmailField()
class ProfileSerializer(dt.Serializer):
    user = UserSerializer()
user1 = User(email="[email protected]")
user2 = User(email="[email protected]")
profile1 = Profile(user=user1)
profile2 = Profile(user=user2)
profiles = [profile1,profile2]
serializer = ProfileSerializer(profiles,many=True)
serializer.data

My main goal when I created this package was to be fast as possible so I had to give up some features to achieve that but I might change that in the future

@DarViSte
Copy link

@Mng-dev-ai Thank you for your advice! I'll try the logic which you've suggested.
Actually looks like using drf-turbo does not help a lot because logic of my serializer is too complicated and all looping and nesting within serializer still take a lot of time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants