Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Technical aspects, customization, code samples.
Post Reply
rosario.dandrea
Posts: 9
Joined: Fri Jan 31, 2020 9:39 am

Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Post by rosario.dandrea »

Hi everyone,

I'm having the following issue happen in the app I'm writing. I'm looking at the tag app to understand what I'm doing wrong, but everything seems to be okay. I get the following error:

Error retrieving X list: object of type 'X' has no len().

View:

Code: Select all

    def get_company_queryset(self):
        return self.get_offer().get_companies(user=self.request.user)
and Model:

Code: Select all

   
   def get_companies(self, user):
        return AccessControlList.objects.restrict_queryset(
            permission=permission_companies_view, queryset=self.company,
            user=user
        )
in attached the screenshot of error.

thanks for your time
Attachments
MayanProblem.png
MayanProblem.png (41.09 KiB) Viewed 1196 times

User avatar
rosarior
Developer
Developer
Posts: 522
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Post by rosarior »

Hi,

the issue is with the input of the .restrict_queryset() method.

Code: Select all

   def get_companies(self, user):
        return AccessControlList.objects.restrict_queryset(
            permission=permission_companies_view, queryset=self.company,
            user=user
        )
The method expects a queryset for its input via the queryset argument, and will return a filtered queryset of the same model based on the permission and the roles and groups of the user. In the example, the queryset is set to a single company, it should be a queryset of the Company model.

Code: Select all

   def get_companies(self, user):
        return AccessControlList.objects.restrict_queryset(
            permission=permission_companies_view, queryset=Company.objects.all(),  # <-----
            user=user
        )
Give this a try and let me know if it works.

User avatar
rosarior
Developer
Developer
Posts: 522
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Post by rosarior »

The "no len()" error comes from the template title calculation. It is trying to calculate how may objects are being shown and if pagination is necessary. Since the view is passing a single company, it fails to obtain the length of the expected queryset.

rosario.dandrea
Posts: 9
Joined: Fri Jan 31, 2020 9:39 am

Re: Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Post by rosario.dandrea »

Hi,

i already did it, but the following exception occurred:

in get_company_queryset
get_companies
permission=permission_companies_view, queryset=self.company.all(), AttributeError: 'NoneType' object has no attribute 'all'.

company field is:
company = models.ForeignKey(Company, blank=True, null=True, on_delete=models.CASCADE, related_name="companies")

thanks for yuor time.
Rosario

rosario.dandrea
Posts: 9
Joined: Fri Jan 31, 2020 9:39 am

Re: Help needed when writing new app, when retrieve data from foreign key no len() exception occurred

Post by rosario.dandrea »

Hi,

I solved the problem in this way:

Code: Select all

def get_companies(self, user):
        return AccessControlList.objects.restrict_queryset(
            permission=permission_companies_view, queryset=Company.objects.filter(pk=self.company.id),
            user=user
        )
I'm posting it here in case someone has my same issue.

Thank you very much for pointing me in the right direction.

Best Regards,
Rosario

Post Reply