3

Seemingly simple situation: Django model has foreign key:

class Invite(models.Model):
    inviter = models.ForeignKey(User, on_delete=models.CASCADE)
    ...

In async context, I do:

# get invite with sync_to_async decorator, then
print(invite.inviter)

Get async's favorite error:

You cannot call this from an async context - use a thread or sync_to_async

print(sync_to_async(invite.inviter)) # -> throws the same error

Sure, I can do:

@sync_to_async
def get_inviter(self, invite):
    return invite.inviter

But, this is senile, if I have to do this for every queryset property call.

Is there a sane way to handle this?

Perhaps, there is a way to do this for all calls like that at once?

Lex Podgorny
  • 2,598
  • 1
  • 23
  • 40

3 Answers3

1

Yes, resolve the extra fields using select_related:

# Good: pick the foreign_key fields using select_related
user = await Invite.objects.select_related('user').aget(key=key).user

Your other string non-foreign such as strings and ints attributes should already exist on the model.

Won't work, (although they feel like they should)

# Error django.core.exceptions.SynchronousOnlyOperation ... use sync_to_async
user = await Model.objects.aget(key=key).user
# Error (The field is actually missing from the `_state` fields cache.
user = await sync_to_async(Invite.objects.get)(key=key).user

Other examples for research

A standard aget, followed by a foreign key inspection yields a SynchronousOnlyOperation error.

I have a string key, and a ForeignKey user to the standard user model.

class Invite(models.Model):
    user = fields.user_fk()
    key = fields.str_uuid()

An example with alternatives that mostly don't work:

Invite = get_model('invites.Invite')
User = get_user_model()

def _get_invite(key):
    return Invite.objects.get(key=key)


async def invite_get(self, key):
    # (a) works, the related field is populated on response.
    user = await Invite.objects.select_related('user').aget(key=key).user
   

async def intermediate_examples(self, key):
    # works, but is clunky.
    user_id = await Invite.objects.aget(key=key).user_id
    # The `user_id` (any `_id` key) exists for a FK
    user = await User.objects.aget(id=user_id)


async def failure_examples(self, key):
    # (b) does not work. 
    user = await sync_to_async(Invite.objects.get)(key=key).user
    invite = await sync_to_async(Invite.objects.get)(key=key)

    # (c) these are not valid, although the error may say so.
    user = await invite.user
    user = await sync_to_async(invite.user)

    # same as the example (b)
    get_invite = sync_to_async(_get_invite, thread_sensitive=True)
    invite = get_invite(key)
    user = invite.user # Error 
    
    # (d) Does not populate the additional model
    user = await Invite.objects.aget(key=key).user # Error
Glycerine
  • 7,157
  • 4
  • 39
  • 65
1
print(sync_to_async(invite.inviter))  # -> throws the same error

That's because it's equivalent to:

i = invite.inviter  # -> throws the error here
af = sync_to_async(i)
print(af)

The correct usage is:

f = lambda: invite.inviter
af = sync_to_async(f)
i = await af()
print(i)

# As a one-liner
print(await sync_to_async(lambda: invite.inviter)())

Is there a sane way to handle this?

Perhaps, there is a way to do this for all calls like that at once?

(Disclaimer: Not tested in production.)

With nest_asyncio, you could do this:

def do(f):
    import nest_asyncio
    nest_asyncio.apply()
    return asyncio.run(sync_to_async(f)())


print(do(lambda: invite.inviter))

Or take it even further:

class SynchronousOnlyAttributeHandler:
    def __getattribute__(self, item):
        from django.core.exceptions import SynchronousOnlyOperation
        try:
            return super().__getattribute__(item)
        except SynchronousOnlyOperation:
            from asgiref.sync import sync_to_async
            import asyncio
            import nest_asyncio
            nest_asyncio.apply()
            return asyncio.run(sync_to_async(lambda: self.__getattribute__(item))())
class Invite(models.Model, AsyncUnsafeAttributeHandler):
    inviter = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    ...
# Do this even in async context
print(invite.inviter)
aaron
  • 39,695
  • 6
  • 46
  • 102
0

Does something like this work? Instead of invite.inviter you do await async_resolve_attributes(invite, "inviter")

@sync_to_async
def async_resolve_attributes(instance, *attributes):
    current_instance = instance
    for attribute in attributes:
        current_instance = getattr(current_instance, attribute)
    resolved_attribute = current_instance
    return resolved_attribute
meg hidey
  • 192
  • 4
  • 15