-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FieldFile is not JSON serializable
Error & custom object serialization
#35
Comments
Good feedback. Some of this requires changes in django-webhook.
You could change the encoder class to a custom JSON encoder. You could either return a string value for the file field or omit the field entirely. See: https://github.com/danihodovic/django-webhook/blob/master/django_webhook/settings.py#L4 # settings.py
DJANGO_WEBHOOK = {"PAYLOAD_ENCODER_CLASS": YourCustomEncoder}
It would require a change in django-webhook.
I think it would require a change in django-webhook in the sense of adding a custom filter function which determines if the webhook should be triggered. The filter function would need the pre and post model field values. Let me know if you're interested in opening a pull-request. |
Thanks for your answer @danihodovic. I think for us the way forward that makes the most sense is the second idea, i.e. the ability to specify a django serializer for each model to use in the webhook. Edit: another subsidiary question: how would we go about adding other fields to the webhook model ? e.g. |
I'll get back to you later this week or the next |
@danihodovic small reminder just in case :) |
I'm having a busy few weeks, will post back soon |
Is it possible to resolve 1) and 2) by providing a custom JSON encoder? # settings.py
from foo.bar.baz import MyCustomEncoder
DJANGO_WEBHOOK = dict(PAYLOAD_ENCODER_CLASS=MyCustomEncoder) # foo/bar/baz.py
from django.core.serializers.json import DjangoJSONEncoder
class MyCustomEncoder(DjangoJSONEncoder):
def default(self, o):
if isinstance(o, FileField):
#... |
I can't seem to make class MyCustomEncoder(DjangoJSONEncoder):
def default(self, o):
raise ValueError("this should always be triggered")
# if isinstance(o, FieldFile):
# raise TypeError("TEST FieldFile is not JSON serializable")
# return o.url
# return super().default(o)
DJANGO_WEBHOOK = dict(
MODELS=["api.Pad"],
PAYLOAD_ENCODER_CLASS=MyCustomEncoder,
) I still got the same Is there perhaps another step I'm missing to enable the custom JSON encoder ? |
Can you give 0.0.15 a try? |
The custom serializer is now indeed taken into account, but I am encountering celery/amqp |
@danihodovic Good news, I have figured things out on my end. A further Celery issue ( I now have a webhook working, as you can see in this example result received by {
"object": {
"is_dirty": true,
"id": 67,
"launch_library_id": 176,
"active": true,
"name": "Ariane Launch Area 4",
"string_name": "Ariane Launch Area 4 | Guiana Space Centre, French Guiana",
"description": "ELA-4, is a launch pad and associated facilities at the Centre Spatial Guyanais in French Guiana. The complex is composed of a launch pad with mobile gantry, an horizontal assembly building and a dedicated launch operations building. ELA-4 is operated by Arianespace as part of the Ariane 6 program.",
"info_url": null,
"wiki_url": "https://en.wikipedia.org/wiki/Guiana_Space_Centre",
"image": 1978,
"map_url": "https://www.google.com/maps?q=5.256319,-52.786838",
"country": 50,
"latitude": 5.256319,
"longitude": -52.786838,
"location": 13,
"map_refresh": false,
"map_scale": 12,
"map_image": "https://thespacedevs-prod.nyc3.digitaloceanspaces.com/media/map_images/pad_67_20200803143559.jpg",
"orbital_launch_attempt_count": 1,
"total_launch_count": 1,
"fastest_turnaround": null
},
"topic": "api.Pad/update",
"object_type": "api.Pad",
"webhook_uuid": "dcdea97e-b2c3-43b8-b00f-b5528832c02a"
} Circling back to my two questions above:
|
Nice to see that you managed to get it working. Maybe we should add some sort of middleware or filter function that allows you to modify the payload before it's sent. What do you think? As a side note I'm curious what you're using django-webhook for. I think you're the first user posting about it. |
I suppose so, yes. I don't know if it's possible to automatically figure out the endpoint serializer, but providing a mapping for each model would be acceptable. Ideally even a set of mappings of which one is chosen in each webhook object to provide some flexibility.
The goal is to set it up for the Launch Library 2 API, where rapid data updates are important to our users (e.g. during rocket launches when it's a matter of seconds) but actual data changes in the db are sparse (sometimes only a handful daily). We hope to replace the most common API calls that reach our servers multiple times per second with just a few webhook calls to our biggest users. Edit: another idea that would be really useful for us is the ability to customize on a webhook-per-webhook basis which model changes should trigger a webhook event. For example some of our users might only care about one or two models, while some would want all of them to have a perfect mirror of our db. |
Here's what I have in mind:
Sample input to the function: payload_dict = dict(
object=model_dict(instance),
topic=topic,
object_type=self.model_label,
webhook_uuid=str(uuid),
) To understand how I test the sent webhook, see https://github.com/getsentry/responses. What do you think? |
FYI I have not forgotten this, on the contrary. I just need to focus on higher priority stuff until late october. Will get back to you then 🙂 |
Hi there,
I have just installed this lib to try it out on a fairly large & complex Django project, and have encountered the following error when saving an object with a
FileField
This leads me to the following questions:
retrieve
serializers used on the endpoints.Any help is greatly appreciated :)
The text was updated successfully, but these errors were encountered: