简体   繁体   中英

Nested serializer with Django-Rest-Framework

I've been trying to use a nested serializer with DRF but it won't display the related item in the output.

Here's my model.py :

class Categorie(models.Model):
    nom = models.CharField(max_length=100)

    def __unicode__(self):
        return unicode(self.nom)

class Item(models.Model):
    nom = models.CharField(max_length=100)
    disponible_a_la_vente = models.BooleanField(default = True)
    nombre = models.IntegerField()
    prix = models.DecimalField(max_digits=5, decimal_places=2)
    history = HistoricalRecords()

    categorie = models.ForeignKey(Categorie, models.CASCADE)


    class Meta:
        verbose_name = "item"
        verbose_name_plural = u"inventaire"

        ordering = ['categorie', 'nom']

    def __unicode__(self):
        return u'{nom} - {nombre}'.format(nom = self.nom, nombre = self.nombre)

and my serializers.py

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Item
        fields = ('nom',)

class CategorieSerializer(serializers.ModelSerializer):
    items = ItemSerializer(many=True)

    class Meta:
        model = Categorie
        fields = ('nom', 'id', 'items')

The view i'm currently testing is very basic :

class InventaireDetail(generics.RetrieveAPIView):
    queryset = Categorie.objects.all()
    serializer_class = CategorieSerializer

but it gives the error:

AttributeError: Got AttributeError when attempting to get a value for field items on serializer CategorieSerializer . The serializer field might be named incorrectly and not match any attribute or key on the Categorie instance. Original exception text was: 'Categorie' object has no attribute 'items'.

I've been looking for a while now but i can't get it working even with the help of the doc.

Categorie.items does not exist. By default the reverse relation would get the name Categorie.item_set . You can fix that in two ways.

EITHER: add related_name to your foreign key.

class Item(models.Model):
    categorie = models.ForeignKey(Categorie, models.CASCADE, related_name='items')

OR: another solution is to change the CategorieSerializer

class CategorieSerializer(serializers.ModelSerializer):
    items = ItemSerializer(many = True, read_only=True, source='item_set')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM