Skip to content

Instantly share code, notes, and snippets.

@NekoTashi
Last active April 12, 2016 06:03
Show Gist options
  • Select an option

  • Save NekoTashi/3445670d533139e034f28fd5d9b4a867 to your computer and use it in GitHub Desktop.

Select an option

Save NekoTashi/3445670d533139e034f28fd5d9b4a867 to your computer and use it in GitHub Desktop.
Ejemplo DRF filter con django_filters.
class GatoFilter(django_filters.FilterSet):
class Meta:
model = Gato
fields = ['user__pk', ]

Instalando django-filter

Instalar con pip:

  • pip install django-filter.
  • Y entonces añadir 'django_filters' a tus INSTALLED_APPS.
  • Uso http://django-filter.readthedocs.org/en/latest/usage.html.
class Gato(models.Model):
user = models.ForeignKey(User)
class GatosSerializer(serializers.ModelSerializer):
class Meta:
model = Gato
fields = '__all__'
class GatoView(mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
""" /api/v1/gato/?user__pk=pk_user """
authentication_classes = (TokenAuthentication, )
serializer_class = GatoSerializer
queryset = Gato.objects.all()
filter_backends = (filters.DjangoFilterBackend, )
filter_class = GatoFilter
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment