I have seen all of the similarly titled questions. Case is different:
The problem arises when running the unittest.
I had to import some foreign tables because they already had data in them (country region city ) data. So I had to create the Model as managed = false
class Iptoc(models.Model):
id_ip = models.IntegerField(primary_key=True)
ip_from = models.IntegerField()
ip_to = models.IntegerField()
country_code2 = models.CharField(max_length=2)
threecode = models.CharField(max_length=3)
country_name = models.CharField(max_length=40)
class Meta:
managed = False
db_table = 'iptoc'
When I do a query in a view it works 100% correct that is, it does use the table.
def index_country(request):
# getting the hostname by socket.gethostname() method
hostname = socket.gethostname()
## getting the IP address using socket.gethostbyname() method
ip_address = socket.gethostbyname(hostname)
## printing the hostname and ip_address
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
x = 18874378
record = Iptoc.objects.get(ip_to__gte=x,ip_from__lte=x)
pais = record.country_name
print(pais)
The table in my postgresql is called iptoc (no capital letters)
When I run this test:
class HomeTests(TestCase):
def test_home_view_status_code(self):
url = reverse('country')
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
I get:
File "/home/alvar/PycharmProjects/Carpeta9Abril/venv9Abril/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "iptoc" does not exist
LINE 1: ... "iptoc"."threecode", "iptoc"."country_name" FROM "iptoc" WH...
>Solution :
For tests, it works with a database with a _test suffix, so if your database is named foo, it will use foo_test for tests.
You thus should create an equivalent table in the test database.