toastdriven/django-tastypie — pull request #537 by jjmaestro (2012-06-19 14:30:39+00:00)
Title: Removing the Manager dependency in ToManyField.dehydrate()

In [fields.py:726](/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L726) `ToManyField.dehydrate()` assumes `the_m2m` to be a Manager. At that point, it only needs a list of objects to dehydrate. Thus, IMHO, it's better and more general to be able to handle a list or tuple of objects.

This way, we can manipulate the list of objects to return in the model layer, in case we need it.

E.g.:
```python
## models.py
import random
from django.db import models

UserProfile(models.Model):
nickname = models.CharField(max_length=100, blank=True, null=True)

Visits(models.Model):
visitor = models.ForeignKey(UserProfile, related_name="visitor")

@property
def guesses(self):
potential_visitors = list(UserProfile.object.all()[:9])
potential_visitors.append(self.visitor)
random.shuffle(potential_visitors)
return potential_visitors

## resources.py
from tastypie import fields
from tastypie.resources import ModelResource

class UserProfileResource(ModelResource):
class Meta(BaseMeta):
queryset = UserProfile.objects.all()

class VisitResource(ModelResource):
visitor = fields.ForeignKey(FriendResource, 'visitor', full=True)
guesses = fields.ToManyField(FriendResource, 'guesses', \
readonly=True, full=True, null=True)

class Meta(BaseMeta):
queryset = Visit.objects.all()[:10]
```