# Create your models here.
class Years(models.Model):
decadence = models.IntegerField(verbose_name="Десятилетие", primary_key=True)
def __str__(self):
"""Возвращает строковое представление модели"""
return str(self.decadence)
class Year(models.Model):
year = models.IntegerField(verbose_name="Год выпуска", primary_key=True)
photo = models.ImageField(verbose_name="Фотография года")
decadence = models.ForeignKey(Years, on_delete=models.CASCADE)
def __str__(self):
"""Возвращает строковое представление модели"""
return str(self.year)
class Person(models.Model):
name = models.CharField(max_length=50, verbose_name="Имя и фамилия")
x_position = models.DecimalField(verbose_name="Отступ слева", default=0, decimal_places=5, max_digits=10)
y_position = models.DecimalField(verbose_name="Отступ сверху", default=0, decimal_places=5, max_digits=10)
year = models.ForeignKey(Year, on_delete=models.CASCADE)
def __str__(self):
return self.name
{
"2010": {
"2011": {
"photo": "img",
"persons": {
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
}
},
"2015": {
"photo": "img",
"persons": {
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
}
},
},
"2020": {
"2022": {
"photo": "img",
"persons": {
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
}
},
"2023": {
"photo": "img",
"persons": {
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
{
"name": "имя",
"x_position": "12"
"y_position": "34
},
}
},
}
}
year = models.ForeignKey(Year, on_delete=models.CASCADE, related_name='persons')
decadence = models.ForeignKey(Years, on_delete=models.CASCADE, related_name='release')
class PersonsSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = '__all__'
class YearsSerializer(serializers.ModelSerializer):
persons= PersonsSerializer(read_only=True, many=True)
class Meta:
model = Year
fields = '__all__'
class DecadenceSerializers(serializers.ModelSerializer):
release = YearsSerializer(read_only=True, many=True)
class Meta:
model = Years
fields = '__all__'
class PersonsViewSet(viewsets.ModelViewSet):
queryset = Years.objects.all()
serializer_class = DecadenceSerializers
http_method_names = ['get']