0

models.py

class Followup(models.Model):
    FOLLOWUP_CHOICES = (
        (0, 'Not Finished'),
        (1, 'Finished')
    )
    lead = models.ForeignKey(
        Lead,
        on_delete=models.CASCADE,
        related_name='followups'
    )
    response = models.CharField(
        max_length=150,
        blank=True, null=True
    )
    response_number = models.SmallIntegerField(
        'Number of given followup response (eg. 1)',
        default=1
    )
    date = models.DateField(auto_now_add=True)
    action = models.SmallIntegerField(
        choices=FOLLOWUP_CHOICES,
        default=FOLLOWUP_CHOICES[0][0]  # not finished item's index.
    )
    recorded_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='recorded_followups'
    )

    def __str__(self):
        return self.lead.name

I have run all migrations locally. Whenever I make some change in my model and I try to run migrations, django complains about this error. There's another case, when I mention this model in the loop of the viewset, Django will raise this exception.

views.py

class FollowupViewSet(viewsets.ModelViewSet):
    # I comment from here
    followups = Followup.objects.all()
    expected_followup_ids = []
    lead_names = []
    for followup in followups:
        if followup.lead.name not in lead_names:
            lead_names.append(followup.lead.name)
            expected_followup_ids.append(
                Followup.objects.filter(lead=followup.lead).last().id
            )
        else:
            continue
    queryset = Followup.objects.filter(id__in=expected_followup_ids)
    # to here
    serializer_class = FollowupSerializer
    filter_backends = [DjangoFilterBackend, ]
    filterset_fields = ['date', 'recorded_by', 'response']

What's wrong here? How can I run migrations without having caused the error and without commenting on my views.py file's code block?

Tareq Monwer
  • 185
  • 1
  • 13
  • Why does your code live directly in the class and not some **method** of the class? Check my [answer](https://stackoverflow.com/a/66684938/14991864) on a similar question. – Abdul Aziz Barkat Apr 08 '21 at 06:04
  • @AbdulAzizBarkat TBH, I'm new in DRF, I just wanted to display unique objects from the related model. I ended up writing these lines of code to provide a qs. Is this why Django complains or I'm not following best practice here? – Tareq Monwer Apr 08 '21 at 06:09
  • You should never evaluate a queryset somewhere on a global level, it only leads to difficulty in migrating and also leads to unintended behavior of your queryset never changing despite your data is changed in the database. Also what database do you use? – Abdul Aziz Barkat Apr 08 '21 at 06:11
  • PostgreSQL on deployment, SQLite in local. Can you please point me out where is the right place to evaluate the modified queryset? – Tareq Monwer Apr 08 '21 at 06:13
  • I got it. the get_queryset. Thanks. – Tareq Monwer Apr 08 '21 at 06:25

0 Answers0