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 54 forks source link

Why the need to subclass a class just to get access to encode_record and encode_data #8

Closed lsemel closed 13 years ago

lsemel commented 13 years ago

Why not allow them to be imported directly? I have an endpoint that looks like this, and it seems to be a lot of rigamarole to have to subclass BaseEndpoint just so I can get access to the encoding function.

def browse_group(request):
    group = get_object_or_404(Group,pk=request.POST['group_id'])
    all_people = Person.objects.order_by('last_name').filter(groups=group)[:10]
    return {'people':_encode_data(all_people)}
joestump commented 13 years ago

I recently broke that out into encoders.py, which might have some of the answers you're looking for. (I agree philosophically with you.)

lsemel commented 13 years ago

How would you use this? I tried importing encoder and calling encoder.encode(all_people), but get an error "'QuerySet' object has no attribute '_meta'

lsemel commented 13 years ago

I made a commit in my fork that enables these to be called standalone, and added a test: https://github.com/lsemel/django-ajax/commit/7738c3bc571fc1871f7b50da6cb3626872cdeed6

I didn't see tests for model endpoints, so wasn't sure how to make sure this didn't break anything.

joestump commented 13 years ago

Sweet, this is pretty close to what I was going to implement. I'm going to recommend that I close this ticket and we move the discussion over to an official Pull Request with the commit you have above. How's that sound?

lsemel commented 13 years ago

Sure, sending pull request now

joestump commented 13 years ago

See https://github.com/joestump/django-ajax/pull/10 for more if you're following along at home.