how to get request from scope #1698
-
I have a problem. In my channels consumer, I send serialized data to the connected client (using drf serializers). async def ready(self):
"""
indicate that the client is in a
connected state and is ready to
receive gateway events.
"""
user = self.scope.get('user')
await self.dispatch_event(
{
'opcode': self.READY,
'data': PartialUserSerializer(user).data
}
) The serializer above has image fields in it. Right now, the serializer cannot build an absolute url for the field because no request was passed in the context. How can I ensure that the image fields of the serializer contain absolute urls? Can I pass in a request variable somehow? Is there any other solution? class PartialUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
read_only_fields = ('is_verified', 'email')
exclude = ('last_login', 'password', 'user_permissions', 'groups') class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
verbose_name=_('email'),
help_text=_("user's email address"),
max_length=255
)
username = models.CharField(
verbose_name=_('username'),
help_text=_("display name"),
unique=True,
db_index=True,
max_length=32,
validators=(
MinLengthValidator(2),
UnicodeUsernameValidator
)
)
avatar = models.ImageField(
verbose_name=_('avatar'),
help_text=_("user's avatar url"),
upload_to='avatars/',
default='avatars/default.jpg',
) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
A scope isn't a request... 🤔 You could use one though to instantiate a |
Beta Was this translation helpful? Give feedback.
-
did you find any solution ?? |
Beta Was this translation helpful? Give feedback.
-
As @carltongibson said you can use Check this code:
|
Beta Was this translation helpful? Give feedback.
A scope isn't a request... 🤔 You could use one though to instantiate a
django.core.handlers.asgi.ASGIRequest
I suppose. (That's just whatASGIHandler
itself will do.)