[Answer]-How to delete a value from 'ValuesListQuerySet' in django?

1👍

✅

Lets say you have a model Device which is assigned to a model User. A new device should only get a user which does not have any device yet.
So we want to exclude the users in your choice field which already have a device:

User.objects.exclude(pk__in=Device.objects.values_list('user', flat=True).distinct())

The inner query gets only the distinct values of the Device’s user field. And we use that list of primary keys to exclude all those users in the outer query.

Be aware that this query has to be reevaluated each time you create such a form. If you only do it once in the form field definition, it will not be updated after new records are created. Look here how to do that: https://stackoverflow.com/a/3420588/640916

Leave a comment