api/students/
, второй для детальной страницы api/students/{pk}
#model.py
class Student(models.Model):
first_name = models.CharField(...)
last_name = models.CharField(...)
# info может быть вычисляемым свойством
@property
def info(self):
# какая-то логика
return info
# serializers.py
class StudentListSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ('id', 'first_name', 'last_name')
class StudentDetailSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ('id', 'first_name', 'last_name', 'info')
# views.py
class StudentViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Student.objects.all()
def get_serializer_class(self):
if self.action == self.__class__.retrieve.__name__:
return serializers.StudentDetailSerializer
if self.action == self.__class__.list.__name__:
return serializers.StudentListSerializer
return serializers.StudentListSerializer
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static_files'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)