Django .only() causing maximum recursion depth exceeded error?

Viewed 1480

I was using Django's .only() construct to query only the columns I need.

ex.

Trainer.objects.filter(id__in=ids).only('id', 'lives')

Here is a quick example I wrote to reproduce this:

class BaseTrainer(models.Model):

    mode = models.IntegerField(help_text="An integer")

    def __init__(self, *args, **kwargs):
    super(BaseTrainer, self).__init__(*args, **kwargs)
    self._prev_mode = self.mode


class Trainer(BaseTrainer):

    name = models.CharField(max_length=100, help_text="Name of the pokemon")
    trainer_id = models.CharField(max_length=10, help_text="trainer id")
    badges = models.IntegerField(help_text="Power of the pokemon (HP)")
    lives = models.PositiveIntegerField(default=sys.maxsize)

    unique_together = ("name", "trainer_id")

    def __init__(self, *args, **kwargs):
        super(Trainer, self).__init__(*args, **kwargs)
        self._temp = self.lives

    @classmethod
    def test(cls):

        t = Trainer.objects.only('trainer_id').all()

        print(t)

I needed only id and name fields, that's certain. And what was the reason for maximum recursion depth exceeded?

1 Answers
Related