Can someone provide an example of working with many-to-many fields of instances of django.db models through django-tastypie and back-a-reational? This is now possible using an intermediate model.
from django.db import models class Author(models.Model): name = models.CharField(max_length=42) class Book(models.Model): authors = models.ManyToManyField(Author, related_name='books', through='Authorship') title = models.CharField(max_length=42) class Authorship(models.Model): author = models.ForeignKey(Author) book = models.ForeignKey(Book)
Here the tastypie resource configuration is possible:
from tastypie import fields, resources class AuthorResource(resources.NamespacedModelResource): books = fields.ToManyField('library.api.resources.AuthorshipResource', 'books') class Meta: resource_name = 'author' queryset = models.Author.objects.all() class BookResource(resources.NamespacedModelResource): authors = fields.ToManyField('library.api.resources.AuthorshipResource', 'authors') class Meta: resource_name = 'book' queryset = models.Book.objects.all() class AuthorshipResource(resources.NamespacedModelResource): author = fields.ToOneField('library.api.resources.AuthorResource', 'author') book = fields.ToOneField('.api.resources.BookResource', 'book') class Meta: resource_name = 'authorship' queryset = models.Authorship.objects.all()
How to save the author associated with several unsaved, but with a single request to our server?
source share