Guest User

Untitled

a guest
Apr 19th, 2012
604
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Backbone-Relational and Django-Tastypie: many-to-many fields operating example
  2. from django.db import models
  3.  
  4. class Author(models.Model):
  5. name=models.CharField(max_length=42)
  6.  
  7. class Book(models.Model):
  8. authors=models.ManyToManyField(Author,related_name='books',through='Authorship')
  9. title=models.CharField(max_length=42)
  10.  
  11. class Authorship(models.Model):
  12. author=models.ForeignKey(Author)
  13. book=models.ForeignKey(Book)
  14.  
  15. from tastypie import fields,resources
  16.  
  17. class AuthorResource(resources.NamespacedModelResource):
  18. books=fields.ToManyField('library.api.resources.AuthorshipResource','books')
  19.  
  20. class Meta:
  21. resource_name='author'
  22. queryset=models.Author.objects.all()
  23.  
  24. class BookResource(resources.NamespacedModelResource):
  25. authors=fields.ToManyField('library.api.resources.AuthorshipResource','authors')
  26.  
  27. class Meta:
  28. resource_name='book'
  29. queryset=models.Book.objects.all()
  30.  
  31. class AuthorshipResource(resources.NamespacedModelResource):
  32. author=fields.ToOneField('library.api.resources.AuthorResource','author')
  33. book=fields.ToOneField('.api.resources.BookResource','book')
  34.  
  35. class Meta:
  36. resource_name='authorship'
  37. queryset=models.Authorship.objects.all()
RAW Paste Data