Queryset with 2 fields of a table - Django Rest Framework

0

I want to make a filter with 2 fields of the user model of django first_name and last_name In this way:

queryset = User.objects.annotate(search_name=Concat('first_name', Value(' '), 'last_name'))
queryset.filter(search_name__icontains='Prueba Pruebita')

But you are not returning the filter if not the entire list of users.

This is my serializer that simply brings the most fields and the password that is only written.

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','username', 'password', 'first_name', 'last_name', 'email', 'code', 'phone')
        extra_kwargs = {'password': {'write_only': True}}

Is something missing? Or is there another way to make a filter with 2 fields of a model?

    
asked by Piero Pajares 30.06.2018 в 19:23
source

1 answer

0

Somehow, it works like this:

fullname = request.data.get('full_name', None)
queryset = User.objects.all()
    if fullname:
        queryset = queryset.annotate(search_name=Concat('first_name', Value(' '), 'last_name'))
        queryset = queryset.filter(search_name__icontains=names)

I hope it serves someone. :)

    
answered by 30.06.2018 / 23:04
source