I have the following two models:
class Resource(models.Model):
identifier = models.UUIDField(default=uuid.uuid4, unique=True)
date_added = models.DateTimeField(default=now)
def __repr__(self):
return f'Resource: {self.identifier}, Time added: {self.date_added}'
class URLResource(Resource):
url = models.URLField()
def __repr__(self):
return f'{self.identifier} URL: {self.url}'
When I run the following commands in the django shell, everything works fine:
> from user_page.models import URLResource
> URLResource.objects.create(url='abc')
# outputs: `a3157372-7191-4d70-a4b1-c6252a2a139c URL: abc`
> URLResource.objects.get(url='abc')
# outputs: `a3157372-7191-4d70-a4b1-c6252a2a139c URL: abc`
However, my seemingly trivial test case fails, even though the similar execution succeeds in the django shell:
from django.test import TestCase
from users.models import CustomUser
from .models import Resource, URLResource, Record
class URLResource(TestCase):
def setUp(self):
url_resource = URLResource.objects.create(url='abc')
print(repr(url_resource))
def test_url(self):
print(repr(URLResource.objects.get(url='abc')))
I get the following error:
======================================================================
ERROR: test_url (user_page.tests.URLResource)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/admin/Desktop/readrrr/user_page/tests.py", line 26, in setUp
url_resource = URLResource.objects.create(url='abc')
AttributeError: type object 'URLResource' has no attribute 'objects'
----------------------------------------------------------------------
You have named your test class the same name as the class you are trying to test. Try renaming it to URLResourceTest