For example if I have models.py like this:
Handler(models.Model):
model1 = ForeignKey(Model1)
model2 = ForeigKey(Model2)
user = models.ForeignKey(User)
For example there are 100 instances of Handler with Model1 id = 1, but Model 2 id for this 100 instances is in range 1 to 5. And when I do something like this:
Handles.objects.filter(model1=1).values_list('model2_id', flat=True)
It returns list of 5 id values or list of 100 id values, which are be repeated? And if it return 100 values is there possibility to remain only one v开发者_如何学Pythonalue for every repeated value?
It will return a list of 100 id values. If you want to get the unique 5 ones, then you can do that in python.
model2_id_uniq_values = list(set(Handles.objects.filter(model1=1).values_list('model2_id', flat=True)))
It may not be the most finely tuned algorithm, and by using a set, you would lose the order. But for your purposes it appears to work.
精彩评论