Skip to content

Instantly share code, notes, and snippets.

@akatreyt
Created February 23, 2015 18:43
Show Gist options
  • Save akatreyt/fddc37adf218f9128a8b to your computer and use it in GitHub Desktop.
Save akatreyt/fddc37adf218f9128a8b to your computer and use it in GitHub Desktop.
from django.db import models
from django.utils.translation import ugettext as _
from colleges.models import College
WEEKDAYS = [
(1, _(u'Monday')),
(2, _(u'Tuesday')),
(3, _(u'Wednesday')),
(4, _(u'Thursday')),
(5, _(u'Friday')),
(6, _(u'Saturday')),
(7, _(u'Sunday')),
]
class Activity(models.Model):
name = models.CharField(max_length=50, blank=False)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
website = models.URLField(blank=True)
telephone = models.CharField(max_length=10, blank=True)
description = models.CharField(max_length=500, blank=True)
college = models.ForeignKey(College, blank=False)
class ActivityTime(models.Model):
day = models.CharField(choices=WEEKDAYS, max_length=1, blank=False)
open = models.TimeField(blank=False)
close = models.TimeField(blank=False)
activity = models.ForeignKey(Activity, blank=False)
from activities_services.models import Activity
from activities_services.models import ActivityTime
from activities_services.models import WEEKDAYS
from rest_framework import serializers
class ActivityTimeSerializer(serializers.ModelSerializer):
class Meta:
model = ActivityTime
class ActivitySerializer(serializers.ModelSerializer):
hours = serializers.SerializerMethodField()
class Meta:
model = Activity
def get_hours(self, obj):
matches = ActivityTime.objects.filter(activity=obj)
if len(matches) == 0:
return []
if len(matches) == 1:
serializer = ActivityTimeSerializer(matches)
else:
serializer = ActivityTimeSerializer(matches, many=True)
return serializer.data
Got AttributeError when attempting to get a value for field `day` on serializer `ActivityTimeSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'day'.
@shaunagm
Copy link

You left IRC just before I replied:

Is hours supposed to contain all of the activitytime data, nested?
If so, you might want to try: http://www.django-rest-framework.org/api-guide/relations/#nested-relationships

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment