question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom queryset Manager with a filter by ForeignKey

See original GitHub issue

I know this is very weird, and I have the feeling it might be related to my specific setup, but basically I’m running into an issue when attempting to invoke the save function it fails not being able to assert the query is a SalesforceQuery.

The weird think is that it only happens with contacts. Other SalesforceModels are properly saved without any hustle.

There are no hard relationships between the contact object and any non-salesforce objects. I basically use it as a “profile” for local users, matching by email address.

Is there anything known that may lead to this?

Here is what I think is the relevant code …

from salesforce import models as sfmodels
from authtools import models as atmodels
from salesforce.backend.manager import SalesforceManager


class CustomManager(SalesforceManager):

    def get_queryset(self, _alias=None):
        return super().get_queryset(_alias=_alias).filter(record_type__developer_name='Custom',
                                                          portal_status__in=('Invited', 'Active'))
...
class Contact(sfmodels.Model):
    ... # regular contact fields in here
    objects = CustomManager()

class User(atmodels.AbstractEmailUser):
    @property
    def srecord(self):
        return Contact.objects.get(email=self.email)

and a sample snippet:

>>> from authentication.models import Contact
>>> c = Contact.objects.get(email="dhbahr@gmail.com")
>>> c.portal_requires_token = True
>>> c.save()
Query
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".env/lib/python3.8/site-packages/django/db/models/base.py", line 740, in save
    self.save_base(using=using, force_insert=force_insert,
  File ".env/lib/python3.8/site-packages/django/db/models/base.py", line 777, in save_base
    updated = self._save_table(
  File ".env/lib/python3.8/site-packages/django/db/models/base.py", line 850, in _save_table
    updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  File ".env/lib/python3.8/site-packages/django/db/models/base.py", line 900, in _do_update
    return filtered._update(values) > 0
  File ".env/lib/python3.8/site-packages/django/db/models/query.py", line 760, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File ".env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1429, in execute_sql
    cursor = super().execute_sql(result_type)
  File ".env/lib/python3.8/site-packages/salesforce/backend/compiler.py", line 89, in execute_sql
    cursor.execute(sql, params)
  File ".env/lib/python3.8/site-packages/salesforce/backend/utils.py", line 183, in execute
    response = self.execute_django(q, args)
  File ".env/lib/python3.8/site-packages/salesforce/backend/utils.py", line 232, in execute_django
    response = self.execute_update(self.query)
  File ".env/lib/python3.8/site-packages/salesforce/backend/utils.py", line 328, in execute_update
    pks = self.get_pks_from_query(query)
  File ".env/lib/python3.8/site-packages/salesforce/backend/utils.py", line 308, in get_pks_from_query
    assert isinstance(pks, Query) and type(pks).__name__ == 'SalesforceQuery'
AssertionError

(Hynekcer: edited the long paths before “.env/”. Django is 2.2)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
hynekcercommented, May 9, 2020

The bug is found and will be easily fixed in the next release. A base manager did depend on the default manager. An explicit base manager is now assigned in the SalesforceModel class, independent on the default manager. The method .save() is executed now again by one HTTP request, as expected.

1reaction
hynekcercommented, Apr 28, 2020

The rest of the issue is fixed by a clear error message
AssertionError: Too complicated queryset.update(). Rewrite it by two querysets. See docs wiki/error-messages

It can be searched in the wiki AssertionError: Too complicated queryset.update()

Because the suggestion in the wiki seems that it solves the rest your problem completely, I’m closing it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access custom QuerySet methods from the Manager of ...
QuerySet ): def by_type(self, type): return self.filter(type=type) ... I think you would be able to access custom queryset method with this ...
Read more >
Managers - Django documentation
Custom managers ¶ ... You can use a custom Manager in a particular model by extending the base Manager class and instantiating your...
Read more >
Django custom manager demystified | by Mohammad Asim Ayub
The published manager is filtering the queryset to return only those posts that are published. This is an example of the second reason...
Read more >
Filtering - Django REST framework
The simplest way to filter the queryset of any view that subclasses GenericAPIView is to override the .get_queryset() method. Overriding this method allows...
Read more >
[Solved]-Using a custom reverse manager in Filter-django
[Solved]-Using a custom reverse manager in Filter-django. Search. score:2 ... ForeignKey(Publisher) is_alive = models.BooleanField(...) objects = models.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found