Created
June 20, 2011 20:50
-
-
Save tijs/1036549 to your computer and use it in GitHub Desktop.
saving an m2m field in one API call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
class Subscriber(models.Model): | |
country = CountryField() | |
first_name = models.CharField(max_length=100, blank=True, null=True) | |
surname = models.CharField(max_length=100, blank=True, null=True) | |
email = models.EmailField() | |
brands = models.ManyToManyField("Brand") | |
#forms.py | |
from django import forms | |
class SubscriberForm(forms.Form): | |
brand = forms.CharField(required=True) | |
email = forms.EmailField(required=True) | |
country = forms.CharField(required=True) | |
first_name = forms.CharField() | |
surname = forms.CharField() | |
# api.py | |
from tastypie.resources import ModelResource | |
from tastypie.validation import FormValidation | |
from subscribers.forms import SubscriberForm | |
from subscribers.models import Subscriber | |
from tastypie.authorization import DjangoAuthorization | |
from tastypie.authentication import BasicAuthentication | |
class SubscriberResource(ModelResource): | |
class Meta: | |
queryset = Subscriber.objects.all() | |
resource_name = 'subscriber' | |
allowed_methods = ['post'] | |
authentication = BasicAuthentication() | |
authorization = DjangoAuthorization() | |
validation = FormValidation(form_class=SubscriberForm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment