0

I have the below table view

CREATE VIEW vStaff
AS
SELECT ContactInfo .*, Staff.position
FROM ContactInfo 
LEFT JOIN Staff ON Staff.name = ContactInfo.name

that selects information from these two models

class ContactInfo(models.Model):
    name = models.CharField(max_length=30)
    email = models.EmailField(max_length=30)
    address = models.CharField(max_length=30)

class Staff(models.Model):
    name = models.CharField(max_length=30)
    position = models.CharField(max_length=30)  

However, when I try to select from the vStaff like below in modelAdmin I get the above error

Staff.objects.annotate(val=RawSQL("SELECT * FROM vStaff", ()))

AttributeError: 'NoneType' object has no attribute 'distinct'

lewis machilika
  • 819
  • 2
  • 11
  • 25
  • 2
    I'm not familiar with `RawSQL`, but are you sure you don't need a "FROM" in your query? As an aside you should never `SELECT *` inside a view because it won't automatically update if you change the table definition. – Dale K Apr 21 '21 at 01:51
  • From is there in `RawSQL` – lewis machilika Apr 21 '21 at 01:53
  • Alright the View itself is fine, as it is able to select required information but the problem is in django `modelAdmin` where I am getting the error – lewis machilika Apr 21 '21 at 01:58
  • why you have `select * vstaff` instead of `select * from vstaff` Now I see this is my typo sorry for that – lewis machilika Apr 21 '21 at 02:04
  • But I am getting the same error anyway. – lewis machilika Apr 21 '21 at 02:04
  • Can you use the debugger or the stack trace to locate the exact line that's giving the error? It is obviously not in any of the code you've posted because there is no reference to an attribute _distinct_. – Nicholas Hunter Apr 21 '21 at 02:58
  • `select * from vStaff` returns neither ContactInfo nor Staff records. Have you tried creating a class with the correct properties? Also I'm not sure how smart Django's parser is, i.e.: you may need to specify `"select name, email, address, position from vStaff"` instead. – AlwaysLearning Apr 21 '21 at 03:24

0 Answers0