python - Django Rest API TypeError when using ChoiceField -


i encountered typeerror when trying use choicefield in serializers.

from rest_framework import serializers rest_framework.fields import choicefield  comp_track_sys.models import campaign contacts.models import clist  class editcampaignserializer(serializers.modelserializer):     class meta:         model = campaign         ...      def __init__(self, *args, **kwargs):         super(editcampaignserializer, self).__init__(*args, **kwargs)         user = self.context['request'].user         clists = clist.objects.filter(company=user.profile.company)         self.fields['clist'] = choicefield(choices=clists) 

i have tried choicefield(choices=json.dumps(clists)), still got error.

typeerror: <clist: testing> not json serializable 

these models in case needed resolve issue.

# campaign model class campaign(models.model):     campaign_id = uuidfield(auto=true)     name = models.charfield("campaign name", max_length=50)     company = models.foreignkey(company)     clist = models.foreignkey(clist, verbose_name="contact list", blank=true, null=true)     user = models.foreignkey(user, blank=true, null=true)  # clist model class clist(models.model):     name = models.charfield("contact list", max_length=255)     description = models.textfield(blank=true, null=true)     contacts = models.manytomanyfield(contact, related_name='con_clists')     company = models.foreignkey(company)     updated = models.datetimefield(auto_now=true)     created = models.datetimefield(auto_now_add=true)     uuid = models.charfield("shortuuid", max_length=50, blank=true, null=true) 

this views

class editcampaignviewset(modelviewset):     queryset = campaign.objects.all()     serializer_class = editcampaignserializer     parser_classes = (multipartparser, formparser)      def list(self, request, type=none, *args, **kwargs):         company = request.user.company         queryset = campaign.objects.filter(company=company,                                            campaign_type=type)\                                    .order_by('-created')\                                    .prefetch_related('user__profile')         serializer = editcampaignserializer(queryset, many=true, context={'request': self.request})         return response(serializer.data) 

i think should using primarykeyrelatedfield rather choicefield, , defining custom get_queryset method on serializer field filter relevant queryset.

from rest_framework import serializers rest_framework.fields import choicefield  comp_track_sys.models import campaign contacts.models import clist  class clistprimarykeyrelatedfield(serializers.primarykeyrelatedfield):      def get_queryset(self):         return self.queryset.filter(            company=self.root.context['request'].user.profile.company         )  class editcampaignserializer(serializers.modelserializer):      clist = clistprimarykeyrelatedfield(         queryset=clist.objects.all(),     )      class meta:         model = campaign         fields = (             'clist',         ) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -