20π
.distinct([*fields])
only works in PostgresSQL.
From distinct documentation
Hereβs the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.
As stated all fields in a record are checked. Mostly likely in your case you are getting records with different field values (more likely a case if you are queries multiple tables ManyToMany or ForeignKey relations).
For consolidating as array you can refer your earlier question Django Query distinct values works but i cant use the query result
6π
names = Staff.objects.order_by('person__full_name').values('person__full_name').distinct()
will give you distinct full names, and you can do a similar thing to get distinct job categories.
These will give you lists of values, not objects themselves, but if I interpret your question correctly then I think these will give you what you want.
- Django REST framework β multiple lookup fields?
- Reading file data during form's clean method
- Django Nested Groups: Groups in Groups
- Python-social-auth with Django: ImportError: No module named 'social_django'
- Call Django celery task by name
1π
Not all database support Distinct(βfield nameβ). So what we can do is use very simple problem solving skill
class vendor_ready_distinct(APIView):
def get(self, request):
obj_readyproduct = vendor_list_readyProduct.objects.all().order_by('mobile_number')
unique_list = []
list = []
for x in obj_readyproduct:
# check if exists in unique_list or not
if x.mobile_number not in list:
unique_list.append(x)
list.append(x.mobile_number)
seri_obj = vendor_list_readyProductSerializer(unique_list, many=True)
return Response(seri_obj.data)
here we are getting the distinct row by using method 1 from geeksforgeeks
- Best practices for preventing Denial of Service Attack in Django
- In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)?
- The default "delete selected" admin action in Django
1π
You can use this code if you want to use DISTINCT with WHERE statement.
staff = Staff.objects.filter(person__full_name="Alex").values('staff_job_categories').distinct().order_by('person__full_name')
- How do I setup messaging and session middleware in a Django RequestFactory during unit testing
- Django form returns is_valid() = False and no errors