How to get the current object inside of a Serializer

45 views
Skip to first unread message

Sylvain Dégué

unread,
Oct 22, 2016, 9:31:19 PM10/22/16
to Django REST framework
Hi, 



class AddressSerializer(serializers.ModelSerializer):
class Meta:
model = Address


class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category


class BusinessSerializer(serializers.ModelSerializer):
class Meta:
model = Business
exclude = [
'date_added', 'date_modified',]

data = BusinessHour.objects.filter(business=)

address = AddressSerializer(
many=False, read_only=True)
category = CategorySerializer(
many=False, read_only=True)
business_hours = BusinessHourSerializer(
data=data)


I want to query the businesshour by current business. BusinessHour has a foreign key Business so i should be able to use it to get only the business hour of that business.

How do I get the current object

__eiNjel __

unread,
Oct 23, 2016, 2:08:51 AM10/23/16
to Django REST framework
it depends how you wrote model for busnesshour serializer. but basicly you want to query "SELECT BusinessHours FROM BusinessHour" where BusinessHours is your data. You can add ".first()"
Reply all
Reply to author
Forward
0 new messages