1

I have a django project with a custom user model.

I have this Subscriptions model that uses the custom user model as its foreign key.

class Subscriptions(models.Model):
    user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    app_subscriptions = models.CharField(max_length=100)

    def __str__(self):
        return self.app_subscriptions

But if I try to retreive all users app_subscriptions with ..app_subscriptions_set.all() it always returns with the following error:

AttributeError: 'CustomUser' object has no attribute 'app_subscriptions_set'

I have the same type of model in use here:

from django.db import models

class Software(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Version(models.Model):
    version = models.CharField(max_length=50)
    software = models.ForeignKey(Software, on_delete=models.CASCADE)

    def __str__(self):
        return self.version

And on this model I have no issues querying for all software versions with ...version_set.last().

Anyone have any ideas? Thank you in advance.

Ralf
  • 16,086
  • 4
  • 44
  • 68

1 Answers1

0

Try using user_instance.subscriptions_set.all() instead of .app_subscriptions_set.all().

The default accesor is the model name, all lowercase, and appends _set. See the docs about it, or this question.

Your model Version is just like that:

software_instance.version_set.all()
Ralf
  • 16,086
  • 4
  • 44
  • 68