mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 18:09:59 +03:00
adding small clarification on nested serializiers via through models
This commit is contained in:
parent
29cbe574a3
commit
5cb335155a
|
@ -569,6 +569,65 @@ For more information see [the Django documentation on generic relations][generic
|
|||
|
||||
## ManyToManyFields with a Through Model
|
||||
|
||||
Example, given the following models:
|
||||
|
||||
class Artist(models.Model):
|
||||
name = models.CharField(max_length=180)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Portfolio(models.Model):
|
||||
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=180)
|
||||
artists = models.ManyToManyField(Artist, through='Calification')
|
||||
|
||||
def __str__(self):
|
||||
return '{} - {}'.format(self.user, self.name)
|
||||
|
||||
|
||||
class Calification(models.Model):
|
||||
portfolio = models.ForeignKey(Portfolio, on_delete=models.CASCADE, related_name='califications')
|
||||
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
|
||||
rate = models.IntegerField(default=0)
|
||||
added_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return '{} - {} - {}'.format(self.portfolio, self.artist, self.rate)
|
||||
|
||||
We could serialize them as follows:
|
||||
|
||||
class ArtistSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Artist
|
||||
fields = ('name', )
|
||||
|
||||
|
||||
class CalificationSerializer(serializers.ModelSerializer):
|
||||
|
||||
artist = ArtistSerializer()
|
||||
|
||||
class Meta:
|
||||
model = Calification
|
||||
fields = ('artist', 'rate', 'added_at')
|
||||
|
||||
|
||||
class PortfolioSerializer(serializers.ModelSerializer):
|
||||
|
||||
artists = CalificationSerializer(many=True, source='califications')
|
||||
|
||||
class Meta:
|
||||
model = Portfolio
|
||||
fields = ('id', 'name', 'user', 'artists', )
|
||||
|
||||
|
||||
It's important to remember that it's easier if we set a ``related_name`` to the foreign_key we want to follow from the main model.
|
||||
If we don't set it we would have to set the attribute ``source`` of the nested serializer ``CalificationSerializer`` as
|
||||
|
||||
artists = CalificationSerializer(many=True, source='calification_set.all')
|
||||
|
||||
By default, relational fields that target a ``ManyToManyField`` with a
|
||||
``through`` model specified are set to read-only.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user