Я использую Django REST для API. Мои модели:
class Region(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.CASCADE)
class Category(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='child', on_delete=models.CASCADE)
class Ad(models.Model):
id = models.IntegerField(primary_key=True, blank=True)
title = models.CharField(max_length=30 , help_text='Title ads' , db_column='data')
date_create = models.DateTimeField(auto_now=True,help_text='Date Create')
description = models.TextField(max_length=300)
region = TreeForeignKey(Region,blank=True , null=True, related_name='Reg',on_delete=models.CASCADE)
category = TreeForeignKey(Category,blank=True , null=True, related_name='ad',on_delete=models.CASCADE)
views = models.IntegerField(blank=True, null=True)
price = models.IntegerField(blank=True, null=True)
Мои сериализаторы:
class AdSerializer(serializers.ModelSerializer):
images = serializers.StringRelatedField(many=True)
class Meta:
model = Ad
fields = ('title', 'description', 'region', 'category','price' , 'date_create','id','views','images')
class CategorySerializer(serializers.ModelSerializer):
ad = AdSerializer(many=True)
child = RecursiveField(allow_null=True,required=False,many=True)
class Meta:
model = Category
fields = ('name','id','ad','child')
class RegionSerializer(serializers.ModelSerializer):
children = RecursiveField(allow_null=True, required=False,many=True)
class Meta:
model = Region
fields = ('name','id' , 'children')
views.py:
class AdViewSet(viewsets.ModelViewSet):
queryset = Ad.objects.all().order_by('-date_create')
serializer_class = AdSerializer
как мне теперь написать urls.py, views.py, чтобы я мог фильтровать косую черту по регионам и категориям? Например http://localhost:8000/{region}/{category}/.
Буду рад любым советам и прошу прощения за мой английский