angularjs - Django Serializers many to many relationship -


i working in small project using angularjs , django. have 2 different models corresponding end points.

class skills(models.model):     skills = models.charfield(max_length=100)      def __str__(self):         return self.skills      class project(models.model): ...     required_skills = models.manytomanyfield(             skills, verbose_name="list of skills") .. 

this copy of skills end point

[     {         "id": 1,         "skills": "css"     },     {         "id": 2,         "skills": "html5"     },     {         "id": 3,         "skills": "java"     } ] 

on front page have form uses $http.post service send data database. when project created, might require 1 skill or multiple skills. here confusion come, can store foreign key sending pk in case id. however, since dealing many many relationship, can not figure out best way proceed. have read documentation , online resources success. also, form has dropdown menu list of skills.i got values using $http.get. please has idea of how solve issue?

http://www.django-rest-framework.org/api-guide/relations/ serializing manytomany in django rest framework (2.3.5) throws valueerror

i had deal similar situation recently. way approached problem override create method in serializer like:

def create(self, validated_data):     serviceoffers_data = validated_data.pop('serviceoffers')      def extract_id(s):       return s['id']      clientfile = clientfile.objects.create(**validated_data)     clientfile.serviceoffers.add(*serviceoffer.objects.filter(id__in=map(extract_id, serviceoffers_data)))      return clientfile 

my post data looks so:

{     name: 'hello',     serviceoffers: [         {             id: 1,             name: 'service offer 1'         },         {             id: 2,             name: 'service offer 2'         }     ] } 

the extract_id function isn't mandatory. used because of structure of incoming json. send list of pk's , rid of extract_ids.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -