I have the following Serializer using the Django REST Framework .
This is what I still have ...
serializer.py
class ProductSerializer(serializers.ModelSerializer): score = serializers.SerializerMethodField('get_this_score') class Meta: model = Product fields = ('id', 'title', 'active', 'score') def get_this_score(self, obj): profile = Profile.objects.get(pk=19) score = [val for val in obj.attribute_answers.all() if val in profile.attribute_answers.all()] return (len(score))
urls.py
url(r'^products/(?P<profile_id>.+)/$', ProductListScore.as_view(), name='product-list-score'),
There are several problems with this piece of code.
1) The stroller pk = 19 is hard-coded, it must be self.kwargs['profile_id']. I tried and tried, but I do not know how to pass kwarg to a method and cannot make profile_id work. that is, I cannot get it from the url.
2) If any of this code is in models? I tried adding to the models, but again I can pass the arguments.
models.py i.e. method class
def get_score(self, profile): score = [val for val in self.attribute_answers.all() if val in profile.attribute_answers.all()] return len(score)
django django-models django-rest-framework
jason
source share