N
N
Nubbin2018-09-19 12:35:56
Django
Nubbin, 2018-09-19 12:35:56

Django RelationShips how to connect correctly?

Hello, there are 2 tables

class Data(models.Model):
    title = models.CharField(max_length=200, default=None, verbose_name="Заголовок")

    def __str__(self):
        return self.title

class DataDetail(models.Model):
    name = models.CharField(max_length=200, default=None)
    data = models.ForeignKey(Data, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

how do i bind table "DataDetail" to "Data"
class DataSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Data
        fields = ('title')


class DataDetailSerializer(serializers.HyperlinkedModelSerializer):
    item = ItemSerializer()

    class Meta:
        model = DataDetail
        fields = ('name', 'data')
- here in the DataSerializer the array from "DataDetailSerializer' is not displayed in any way

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FulTupFul, 2018-09-19
@FulTupFul

so include item in fields

class DataDetailSerializer(serializers.HyperlinkedModelSerializer):
    item = ItemSerializer()
    data = DataSerializer()
    class Meta:
        model = DataDetail
        fields = ('data', 'name', 'data')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question