Home > Net >  How Can I Sort These Many To Many Values Without a Through Table?
How Can I Sort These Many To Many Values Without a Through Table?

Time:01-04

I am trying to compare the values in two manytomany fields...this almost works...

author_confirm = Author.objects.filter(id=self.object.update_reader_id).values_list('author_confirm').order_by('pk')
author = Author.objects.filter(id=self.object.update_reader_id).values_list('author').order_by('pk')
authors_are_equal = list(author) == list(author_confirm)
authors_are_not_equal = list(author) != list(author_confirm)

This works in that it gets me the values...but doesn't seem to be cooperating with the order_by...I currently have both fields with identical values...but their PKs are transposed so it tells me these fields are not identical...which is technically correct...but I see the problem is that the PKs are not listed in order...Is there a way to do this without a Through Table?

I am using UUIDs as the primary key....I'm not sure if this is relevant or not...but nonetheless I can't seem to get the values in an ordered way.

Thanks in advance for any ideas.

CodePudding user response:

You should order by the author__pk and author_confirmed__pk, otherwise you are only ordering by the author object itself, which we lready know: that is the self.object.update_reader_id, hence the problem:

author_confirm = (
    Author.objects.filter(id=self.object.update_reader_id)
    .values_list('author_confirm')
    .order_by('author_confirm__pk')
)
author = (
    Author.objects.filter(id=self.object.update_reader_id)
    .values_list('author')
    .order_by('author__pk')
)
  • Related