Django Queryset not working after tranformation to Abstract class child

For more than a year, I construct many of my query using a Django Model where I have simple relation between none abstract model objects and everything was working perfectly :

class Taxon(models.Model):
    parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    rank = models.CharField(max_length=200, unique=False, default="NA")
    name = models.CharField(max_length=200, unique=False, default="NA")

class Sequence(models.Model):
    taxon = models.ForeignKey(Taxon, null=True, blank=True, on_delete=models.CASCADE)
    amplicon = models.CharField(max_length=1000, unique=False)
    sequence = models.CharField(max_length=1000, unique=False)
    score = models.FloatField()

Exemple of queryset using my Model :

taxon.sequence_set.filter(stuff_filtering)

But recently I had to apply some modifications on my database and my Sequence objects are now derivated from an Abstract class called Observation, becoming this

class Observation(models.Model):
    taxon = models.ForeignKey(Taxon, null=True, blank=True, on_delete=models.CASCADE)

    class Meta:
        abstract = True

class Sequence(Observation):
    amplicon = models.CharField(max_length=1000, unique=False)
    sequence = models.CharField(max_length=1000, unique=False)
    score = models.FloatField()

    def __str__(self):
        return str((self.amplicon, self.sequence, self.score, self.taxon))

I didn't changed anything in my queryset because django documentation about abstract class (https://docs.djangoproject.com/en/3.2/topics/db/models/) say you can still use your chlid_set in a query. However i'm facing this error and I couldn't understand why.

AttributeError: 'QuerySet' object has no attribute 'sequence_set'

Knowing that in case of Abstract base classes, a table should be created in db for each child with parent attribute... So i have Sequence object with linked Taxon object

Apparently the solution to this problem if to retrieve the Taxon object with a get and not a filter (event if only one object is returned).

taxon = Taxon.objects.filter(...)  

returning just one value became

taxon = Taxon.objects.get(...)

It's seems a pretty mysterious for me, as I still don't understand why it has worked perfectly with my previous model

Back to Top