Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework filter ignores int field that is mapped to enum

I have what I hope is a simple query for the stack overflow community.

Given the following configuration, I have a fairly simple int field on my "Totals" model and can't seem to get filtering to work on that field.

Here is the object from 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 is the router info from urls.py:

router = DefaultRouter()
router.register(r'totals', TotalViewSet)    
urlpatterns = patterns('',
    url(r'^', include(router.urls))
)

Here is my 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 is the view configuration:

class TotalViewSet(viewsets.ReadOnlyModelViewSet):        
    filter_fields = ('total_type', 'location')    
    queryset = Total.objects.all()
    serializer_class = TotalSerializer

The problem:

  1. A request for all "totals" works fine: GET /totals returns all.
  2. A request for "totals" by location works fine: GET /totals?location=1 returns all totals for location 1.
  3. A request for totals by total_type returns 0 results, incorrectly: GET /totals?total_type=1 returns 0 results. No error thrown.

Looking at the debug toolbar, I can see that no query is executed using the Total model. It makes the queries against django_Session and auth_user and that is it. These queries return as expected.

If I call with both params (location and total_type), I can see that it makes the query with only the location in the WHERE clause, but the API still returns no results...even the the query does (although erroneously).

Any ideas??

Django 1.6.4 Python 2.7 djangorestframework 2.3.13

like image 684
smercer Avatar asked Oct 19 '25 03:10

smercer


1 Answers

Answer (which struck me 2 minutes after I posted the question):

I needed to use the enum values in my choices, not the actual objects that represent the name/value pairs.

Like this:

TOTAL_TYPES = (
    (TotalType.daily_sum.value, 'Daily Sum'),
    (TotalType.weekly_sum.value, 'Weekly Sum')
)

I'm surprised that everything else worked except for the REST filtering params before I made this change.

like image 186
smercer Avatar answered Oct 21 '25 16:10

smercer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!