Giter Site home page Giter Site logo

Comments (22)

skorokithakis avatar skorokithakis commented on July 22, 2024

That's a good idea, do you see anything breaking from changing serializers? What about people who expected an exception and implemented their own serializers instead, will those break?

from django-annoying.

xblitz avatar xblitz commented on July 22, 2024

+1 on this... encoding Decimals is a must for me..

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

This is possible now. You can add into your settings:

FORMAT_TYPES = {
    'application/json': lambda response: json.dumps(response, default=date_time_handler),
    'text/json':        lambda response: json.dumps(response, default=date_time_handler),
}

And use the serializer you want, including Django's. Can someone add a (tested) example in the docs so I can close this issue?

from django-annoying.

pawelnowak avatar pawelnowak commented on July 22, 2024

Slightly off-topic, but could you explain why the 0.7.9 version of django-annoying available through pypi (https://pypi.python.org/pypi/django-annoying/0.7.9) differs from the 0.7.9 version of code available here, on github? Don't they periodically refresh packages in the index?

As a result of that when you install django-annoying using pip you get an older version of the package that, among other things, lacks the support of settings-level customization of the FORMAT_TYPES dictionary.

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

That's very odd, you are right. It does differ. Maybe I forgot to bump the version? I will make a new release, thanks for the heads up.

from django-annoying.

cezio avatar cezio commented on July 22, 2024

#16 (comment)

I believe point of django-annoying is to eliminate certain annoyances in Django framework;) Need of specifying additional settings (that would be a hundredth something setting in a project) doesn't seem to be aligned with the goal above for me.

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

@cezio, sure, but do you have a better idea?

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

My concern here is that it will break things for current users. I do agree that the better serializer should be used by default...

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

How about the usual approach of creating a new decorator, and deprecating the old one?

(edited because mistyped, and sent too early)

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

Hmm, that's an idea, but the new decorator would necessarily be misnamed a bit, so I'm not a huge fan of this practice...

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

True. How about a configuration option that defaults to the old behavior:

@ajax_request(use_django_encoder=True)
def cost_view(request):
    return {'total': Decimal("4")}

@ajax_request()
def user_view(request):
    return {'id': 7}

Arguably, though, it's misnamed already β€” at least it confused me, since AJAX is just one particular way for a client to access the API. This decorator is really for API views, providing machine-readable representations of a resource, no? I.e. api_view or api_response might not be a bad fit. Or, more behavior-related names: encode_response, encode_return_value, … well, just some thoughts. :)

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

The extra parameter still breaks backwards compatibility, since it's not called with parameters by default, but I really like the alternative names. Something like json_response might be the most fitting, being very literal.

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

But it does support YAML, too, no? That's why I shed away from the JSON name. :)

Also, Decorators that can be applied with and without arguments are possible, i.e. you don't have to break backwards-compatibility. You β€œmerely” need to consider the type of the first argument, if it's a function or not. If you always use keyword args for the arguments, then it would be perfectly safe, too. Might not be the most beautiful solution on your side of the code, but if you don't want to change the name, and still fix this issue, you can. :)

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

Yeah, you're right. I didn't consider that we'd never be accepting function arguments in normal usage, so I'll go with your proposed fix above. If anyone wants to beat me to a PR, you are very welcome to.

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

Just had to serialize something to JSON, and wanted to try out the built-in serializer. Turns out it is restricted to Django models. Also, a serialize-deserialize does not yield the same objects: For example, decimals stay a string.

So, I don't think this annotation should be switching to Django's serializer. At least I use API views for more than just plain Django models. If I'm missing something, do tell. :)

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

@mknecht, have you found something that the default json module will serialize but Django's JSON encoder won't? Decimals do stay a string, but the default JSON encoder produces an exception, so I'm not sure that's a valid comparison.

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

@skorokithakis It appears that the serializer will only serialize Django models. But, I would like to use the decorator for primitive object graphs, too, like a dict.

The linked documentation indicates that, and I tried and it refused to serialize dicts and such because they did not have a _meta attribute.

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

That's odd, I've been using it for proper serialization of dates and it's worked fine. I'll investigate.

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

Maybe I misunderstood something. Thought it odd, too. I'd be glad to hear it works / what I did wrong. :)

from django-annoying.

mknecht avatar mknecht commented on July 22, 2024

No, you're right, works just fine. I must have messed something up. Sorry for the confusion. :)

from django-annoying.

troyleesmith avatar troyleesmith commented on July 22, 2024

Yo is it possible to have this issue closed up?

from django-annoying.

skorokithakis avatar skorokithakis commented on July 22, 2024

Certainly, PRs are welcome. I am a bit swamped right now :/

from django-annoying.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.