joestump / django-ajax

A simple framework for creating AJAX endpoints in Django.
http://github.com/joestump/django-ajax
BSD 3-Clause "New" or "Revised" License
260 stars 53 forks source link

Model adhoc endpoints #24

Closed toast38coza closed 12 years ago

toast38coza commented 12 years ago

Hi joestump

I did a tiny tweak that loosened up urls.py to allow adhoc methods on Models.

For example, you can now add methods to classes that extend ModelEndpoint and these will be available at:

/ajax/{app}/{model}/{method}.json - in the Model context or /ajax/{app}/{model}/{pk}/{method}.json - in the Record context

This is useful because it allows you to make your endpoints a little more hierarchical.

For example: if you wanted to get a list of models (let's say, a list of Categories) instead of having and ad-hoc: /ajax/{app}/list_all_categories.json

You can now have something like: /ajax/{app}/{model}/list.json (which would call the list() method in the CategoryEndpoint class) .. which I think is a little neater?

Similarly on the Record level, if you have Category and Books, you might want to get all books by Category. Previously you would need an endpoint something like: /ajax/{app}/get_books_by_category.json

Now you could have something like: /ajax/{app}/category/{pk}/books.json (which would call the books() method in the CategoryEndpoint class)

I suspect you might have kept the functionality locked down on purpose, so if that is the case, feel free to chuck this :).

I updated the README.txt as well.

I also updated the bit about BaseEndpoint.encode_* I may be wrong, but I think that has been deprecated for: ajax.encoders.encoder.encode()

joestump commented 12 years ago

Great addition. Thanks a lot!