Q:

Django Rest Framework - serializing a ManyToManyField with the default 'through'

Hello I'm new to DRF and I can serialize a table with a foreign key, but when it comes to a many to many field, in this case:

Package <> Package_product_atom <> Product_atom

How would I go around serializing this? So far what I'm trying is:
class Product_atomSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product_atom
        ready_only = True
        fields = ('id','title', 'title_override', 'price', 'price_override', 'description', 'description_override','image', 'image_override' ,'tags', 'product_alt', 'date_modified', 'date_created')

class PackageSerializer(serializers.ModelSerializer):
    atomics = Product_atomSerializer(many=True)
    class Meta:
        model = Package
        ready_only = True
        fields = ('id', 'title', 'price', 'discount', 'image', 'tags', 'atomics', 'date_modified', 'date_created')

My models are:
class Product_atom(models.Model):

    title = models.CharField(max_length=255, blank=False)
    title_override = models.BooleanField(default=False, blank=False)
    price = models.SmallIntegerField(default=0, blank=False)
    price_override = models.BooleanField(default=False, blank=False)
    description = models.TextField(blank=True)
    description_override = models.BooleanField(default=False, blank=False)
    image = models.ImageField(upload_to = 'uploads', default = '', blank=True)
    image_override = models.BooleanField(default=False, blank=False)
    tags = models.CharField(max_length=255, blank=False)
    active = models.BooleanField(default=True, blank=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    product_alt = models.ForeignKey(Product_alt, related_name='atomics', on_delete=models.CASCADE)
    default_provider = models.ForeignKey(Provider, on_delete=models.CASCADE)

    def __str__(self):
        return "{}".format(self.title)

class Package(models.Model):

    title = models.CharField(max_length=255, blank=False)
    price = models.SmallIntegerField(default=0, blank=False)
    discount = models.SmallIntegerField(default=10, blank=False)
    image = models.ImageField(upload_to = 'uploads', default = '', blank=True)
    tags = models.CharField(max_length=255, blank=False)
    active = models.BooleanField(default=True, blank=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    zone = models.ForeignKey(Zone, on_delete=models.CASCADE)
    product_atom = models.ManyToManyField(Product_atom)

    def __str__(self):
        return "{}".format(self.title)

Which would work if it was a ForeignKey relation, but I haven't been able to run this with a ManyToManyField, the error I'm getting is:

AttributeError: Got AttributeError when attempting to get a value for field atomics on serializer PackageSerializer.
  The serializer field might be named incorrectly and not match any attribute or key on the Package instance.
  Original exception text was: 'Package' object has no attribute 'atomics'.

I had this error before when serializing a one to many relation, but using related_name fixed it. Would putting a custom model using through and adding a related_name to those fix it? Thank you!

A:

Have you tried changing the variable name 'atomics' to 'product_atom'? 
According to your Package class, 'product_atom' does not have a related name 'atomics'.

