-
Notifications
You must be signed in to change notification settings - Fork 13
[BUG] Unable to retrieve media file in development #22
Description
Hey! Thank you for your work, really appreciate it.
I'm reposting an issue I recently created on the main repo, hope you can help me.
Describe the bug
I have a model like so:
class Organization(models.Model):
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
logo = models.ImageField(upload_to=ORGANIZATION_LOGO_PATH)My settings.py has the following MEDIA_URL:
MEDIA_URL = 'media/'And my main urls.py has the following logic:
urlpatterns = [
...
]
# Serve static and media files in development (insecure for production!)
if settings.DEBUG is True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)As recommended by the docs.
I want my Ninja endpoint to return the full URL of the image in development so that I can use it in an external application.
This configuration works to display images on Django's templates, and in DRF, given that the request was passed to the context, like so:
serialized_data = OrganizationSerializer(organizations, many=True, context={"request": request}).dataUnfortunately, I am not being able to make it work with Ninja. The docs have no reference to this, and the only stuff I found were these two closed issues:
- ImageField missing complete URL? vitalik/django-ninja#534
- Include automatic related model lookup URL, file upload question. vitalik/django-ninja#193
Which tells me to use the full URL instead of just /media. I've tried that, setting the MEDIA_URL variable to 'http://localhost:8000/media/' or 'http://127.0.0.1:8000/media/', but it does not work, I get a 404.
Help would be appreciated!
Versions:
- Python version: [e.g. 3.10.0]
- Django version: [e.g. 5.0.3]
- Django-Ninja version: [e.g. 1.3.0]
- Pydantic version: [e.g. 2.10.6]