Search code examples
pythonsqldjangorelational-databaserawsql

executing raw sql in django error: relation does not exist


I am attempting to execute a raw sql query and I am getting the following error:

relation "venue" does not exist

the query is as follows:

 cityList = Venue.objects.raw("SELECT DISTINCT city FROM Venue")

the model I am grabbing from looks like this:

class Venue(models.Model):
name = models.CharField(max_length=150, blank=False)
description = models.CharField(max_length=1000)
image = models.ImageField(upload_to=imgUnique('venueMedia/venueImages'))
streetAddress= models.CharField(max_length=100)
city = models.CharField(max_length=100, blank=False)
state = models.CharField(max_length=100, blank=False)

This is a syntax error, and I am having a hard time finding the right documentation regarding the proper syntax. There seems to be alot on curser queries but I am not ready to give up on this yet


Solution

  • I don't know the raw SQL. The Django ORM can do this like.

    citylist = Venue.objects.values('city').distinct()

    Doku here.