Search code examples
djangodjango-modelsdjango-querysetdrf-queryset

how to convert a QuerySet to a set in django?


I have a query, name_phonenum = person.objects.value_list('first_name','phone_num') I want to convert the queryset to a set. Any idea how can do it.


Solution

  • You can wrap it over a set(…):

    set(person.objects.value_list('first_name','phone_num'))

    But you can let the database do the work with the .distinct(…) method [Django-doc]:

    person.objects.value_list('first_name','phone_num').distinct()

    this will minimize the bandwidth from the database to the application layer.