Hi, so I’m writing tests for a project, and the function that I’m using to populate the default values for the field seems to be unaware of the creation of other objects. Specifically, in my models.py:
def defaultTags():
""" Returns a list of the default tags we think users should have """
print(Tag.objects.all()) # returns <QuerySet []> during testing
return Tag.objects.filter(selectedDefault = True) # also returns <QuerySet []>
# Our basic user class. The AbstractUser class already implements names, email, & username/password
class User(AbstractUser):
""" User Model - extends AbstractUser Model """
# Whether they're student, faculty, or community
TYPES = TYPES # Need this so User.TYPES works later
type = models.CharField(choices=TYPES, max_length=3, blank=False, default="COM")
likedEvents = models.ManyToManyField('Event', blank=True, related_name="likedUsers")
interestedTags = models.ManyToManyField('Tag', blank=True, default=defaultTags)
I want a user that gets created without any interestedTags provided to just be set to all the tags with selectedDefault as true. I assume it’s not going to change anything, but here’s my Tag model:
class Tag(models.Model):
""" A model for event filtering tags """
name = models.CharField(max_length=32, unique=True)
selectedDefault = models.BooleanField(default=False)
Now, in terms of the test, I create a Tag, and it shows up in the database there, but not when I get the tags in defaultTags:
class APITestCase(TestCase):
""" Tests for the backend API """
def setUp(self):
self.time = timezone.now()
self.tag = Tag.objects.create(name = "Interesting Events", selectedDefault = True)
print(Tag.objects.all()) # returns <QuerySet [<Tag: Tag object (1)>]>
self.user1 = User.objects.create_user(username="admin", password="admintest", type = 'STU')
print(self.user1.interestedTags.all()) # returns <QuerySet []>
I need to be able to have the default tags update based on the tag default value. Any advice on how to fix this would be appreciated, and let me know if there’s more info I can provide.