Skip to content

Instantly share code, notes, and snippets.

View MacMaru's full-sized avatar

MacMaru MacMaru

  • Utrecht, The Netherlands
View GitHub Profile
@MacMaru
MacMaru / django-tastypie-resources-save_m2m.py
Created December 10, 2012 09:53
Custom save_m2m for Django Tastypie to pass in request.user
def save_m2m(self, bundle):
"""
Handles the saving of related M2M data.
Due to the way Django works, the M2M data must be handled after the
main instance, which is why this isn't a part of the main ``save`` bits.
Currently slightly inefficient in that it will clear out the whole
relation and recreate the related data as needed.
"""
@MacMaru
MacMaru / tastypie-m2m.py
Created December 5, 2012 13:22
Stub for Tastypie ManyToMany and passing request user to Django to do custom filtering.
'''
I've received several requests for examples of my pull request for tastypie.
https://github.com/toastdriven/django-tastypie/pull/214
'''
# in your models.py for instance:
class TimeLogManager( models.Manager ):
use_for_related_fields = True
def for_user(self, user):
@MacMaru
MacMaru / wrap_view.py
Created July 31, 2011 16:59
Override Tastypie wrap_view() to return custom (JSON) response
class YourResource(ModelResource):
def wrap_view(self, view):
"""
Wraps views to return custom error codes instead of generic 500's
"""
@csrf_exempt
def wrapper(request, *args, **kwargs):
try:
@MacMaru
MacMaru / searchresource_api.py
Created June 27, 2011 10:26
Tastypie (non-model) Resource example with a listfield populated with m2m data
class SimpleOwnerAuthorization(Authorization):
'''
Does what it says: filters objects by their owner (user).
'''
def __init__(self, ownerfilter=None, *args, **kwargs):
self.ownerfilter = ownerfilter # the user field i.e. 'person__user'
def is_authorized(self, request, object=None):
return True # for now