Django - AttributeError: Manager is not accessible via ManagerLog instances

4

I am new to python, I am currently developing an application, which is responsible for generating several very simple reports, however there is a report which I have not yet been able to solve, I have researched but I still can not find the solution.

Basically what I want to do is the following SQL query through the ORM:

select log.* from   file_managerfiles as file, log_managerlog as log where log.fileLog_id = file.id and file.lastUpload = TRUE

the models are the following:

class ManagerFiles(models.Model):
    filesUpload = models.FileField(upload_to='log', unique=True)
    processingStatus = models.BooleanField(default=False)
    lastUpload = models.BooleanField(default=True)

class ManagerLog(models.Model):
    date = models.CharField(max_length=10)
    time = models.CharField(max_length=30)
    serialNumber = models.CharField(max_length=30)
    sessionID = models.CharField(max_length=50)
    moduleName = models.CharField(max_length=50)
    operationName = models.CharField(max_length=100)
    operationSpecific = models.TextField(max_length=500)
    fileLog = models.ForeignKey(ManagerFiles)

Within the last model (ManagerLog) create a method that returns all records of the ManagerLog model where ManagerFile.lastUpload equals TRUE:

# Report Last Upload File
def report_last_upload(self):
    return self.__class__.objects.filter(ManagerFiles__lastUpload=True)

However when calling this method (report_last_upload) in my view, I get the following error:

  

raise AttributeError ("Manager is not accessible via% s instances"%   cls. name ) AttributeError: Manager is not accessible via ManagerLog   instances

I do not understand what the problem is, I hope you can help me, and thank you in advance

    
asked by Leonardo Antonio Garcia Olmos 14.12.2017 в 13:23
source

1 answer

1

The problem is that the field that makes the relation with ManagerFiles is called fileLog according to how you have defined it in your model. Therefore, the correct way to filter it should be:

def report_last_upload(self):
    return self.__class__.objects.filter(fileLog__lastUpload=True)

You have to use the name of the defined field and not the name of the model you are referencing.

    
answered by 14.12.2017 в 16:20