9

So I basically have this simple model:

class BaseLesson(models.Model):
    YOUTUBE_VIDEO = '0'
    MARKDOWN = '1'

    TYPE_CHOICES = (
        (YOUTUBE_VIDEO, 'youtube-video'),
        (MARKDOWN, 'markdown'),
    )

    type = models.CharField(
        max_length=10, choices=TYPE_CHOICES, default=MARKDOWN, verbose_name=_('type'))

    shown_users = models.ManyToManyField(
        User, related_name='lessons', verbose_name=_('shown users'), blank=True)

    objects = managers.BaseLessonManager()

There is a many-to-many relationship with the User model in shown_users

And I wanna annotate the is_shown status based on the many-to-many table, so I did this:

class BaseLessonManager(InheritanceManager, CachingManager):
    def get_lesson_with_is_shown(self, user):
    
        shown_user_case = django_models.Case(
            django_models.When(shown_users__id=user.id,
                               then=django_models.Value(True)),
            default=django_models.Value(False),
            output_field=django_models.BooleanField())

        return self.get_queryset().annotate(
            is_shown=shown_user_case)

The problem with this is that if user1 and user2 saw the same lesson it will be duplicate, for example:

+-----------------+-----------+
|    lesson_id    |  user_id  |
+-----------------+-----------+
|        1        |     1     |
|        1        |     2     |
|        1        |     3     |
+-----------------+-----------+

For such case, I will get these duplicated lessons:

            {
                "id": 1
                "type": "0",
                "is_shown": true
            },
            {
                "id": 1
                "type": "0",
                "is_shown": false
            },
            {
                "id": 1
                "type": "0",
                "is_shown": false
            }

So it's checking each related lesson field in the SHOWN_USERS table... sample photo: https://i.stack.imgur.com/y2igr.jpg

What I tried so far:

1. Exclude:

I added an exclude expression to get rid of the extra lessons:

return self.get_queryset().annotate(
       is_shown=shown_user_case).exclude(
       django_models.Q(is_shown=False) & django_models.Q(shown_users__id=user.id))

And I think this is super ugly cuz if I have 1000 users and 50 lessons it means I'm taking all 50000 fields then filter 50 of them :(

Is there any cleaner way to do that ?

2. Distinct:

I have tried distinct before and it's not fixing the problem, instead of shown the lesson three times it will show:

  • once (is_shown = True),
  • and another time (is_shown = False)
10
  • 1
    did u try .distinct() with the name of the field you want to be unique? looks like in your case .distinct('lesson_id')
    – Chiefir
    May 24, 2018 at 16:43
  • Note - above recipe will work only withPostgreSQL
    – Chiefir
    May 24, 2018 at 16:50
  • I have tried distinct before and it's not fixing the problem, instead of shown the lesson three times it will show it once (is_shown = True), and another time (is_shown = False) :( May 24, 2018 at 19:38
  • What field do u use distinct on?
    – Chiefir
    May 24, 2018 at 21:00
  • I just added distinct at the end of the query since I'm running in development (sqlite), but as I said let's say we have two repeated lessons... it will basically take one of them as (is_shown=True) and another one as (is_shown=False). I tried to tackle this using prefetch_related(shown_users) with a filter based on id... same result ! May 24, 2018 at 21:23

1 Answer 1

9

I managed to fix the problem, and If figured out that my way of querying many-to-many is the problem, instead of shown_users__id == user.id I used id__in==user.lessons.values('id'), full code:

class BaseLessonManager(InheritanceManager, CachingManager):
    def with_is_shown(self, user):

        user_shown_lessons = user.lessons.values('id')

        shown_user_case = django_models.Case(
            django_models.When(id__in=user_shown_lessons,
                           then=django_models.Value(True)),
            default=django_models.Value(False),
            output_field=django_models.BooleanField())

        return self.get_queryset().annotate(
            is_shown=shown_user_case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.