python - django rest framework filter ignores int field that is mapped to enum -
i have hope simple query stack overflow community.
given next configuration, have simple int field on "totals" model , can't seem filtering work on field.
here object models.py:
class totaltype(enum): daily_sum = 1 weekly_sum = 2 class total(models.model): total_types = ( (totaltype.daily_sum, 'daily sum'), (totaltype.weekly_sum, 'weekly sum') ) location = models.foreignkey(location, db_column='locationid') ci_timestamp = models.datetimefield(db_column='ci_timestamp', blank=false, null=false) amount = models.decimalfield(max_digits=12, decimal_places=2, blank=false, null=false) total_type = models.integerfield(max_length=5, blank=false, choices=total_types) here router info urls.py:
router = defaultrouter() router.register(r'totals', totalviewset) urlpatterns = patterns('', url(r'^', include(router.urls)) ) here object serializer:
class totalserializer(serializers.hyperlinkedmodelserializer): location = serializers.hyperlinkedrelatedfield(view_name="location-detail", many=false) class meta: model = total fields = ('id', 'location', 'ci_timestamp', 'amount', 'total_type') finally, here view configuration:
class totalviewset(viewsets.readonlymodelviewset): filter_fields = ('total_type', 'location') queryset = total.objects.all() serializer_class = totalserializer the problem:
a request "totals" works fine: /totals returns all. a request "totals" location works fine: /totals?location=1 returns totals location 1. a request totals total_type returns 0 results, incorrectly: /totals?total_type=1 returns 0 results. no error thrown.looking @ debug toolbar, can see no query executed using total model. makes queries against django_session , auth_user , it. these queries homecoming expected.
if phone call both params (location , total_type), can see makes query location in clause, api still returns no results...even the query (although erroneously).
any ideas??
django 1.6.4 python 2.7 djangorestframework 2.3.13
answer (which struck me 2 minutes after posted question):
i needed utilize enum values in choices, not actual objects represent name/value pairs.
like this:
total_types = ( (totaltype.daily_sum.value, 'daily sum'), (totaltype.weekly_sum.value, 'weekly sum') ) i'm surprised else worked except rest filtering params before made change.
python django django-models django-views django-rest-framework
No comments:
Post a Comment