2012-04-11 21 views
8

मैं एक Django tastypie एपीआई का निर्माण कर रहा हूँ करने के लिए एक कई लोगों के लिए तत्व जोड़, और मैं, models.py ManyToMany रिश्तोंTastypie, कई रिश्ते

में तत्वों को जोड़ने

उदाहरण के साथ एक समस्या है

class Picture(models.db): 
    """ A picture of people""" 
    people = models.ManyToManyField(Person, related_name='pictures', 
     help_text="The people in this picture", 
    ) 

class Person(models.db): 
    """ A model to represet a person """ 
    name = models.CharField(max_length=200, 
     help_text="The name of this person", 
    ) 

संसाधन:

class PictureResource(ModelResource): 
    """ API Resource for the Picture model """ 
    people = fields.ToManyField(PersonResource, 'people', null=True, 
     related_name="pictures", help_text="The people in this picture", 
    ) 
class PersonResource(ModelResource): 
    """ API Resource for the Person model """ 
    pictures = fields.ToManyField(PictureResource, 'pictures', null=True, 
     related_name="people", help_text="The pictures were this person appears", 
    ) 

मेरे समस्या यह है कि मैं अपने चित्र संसाधन में एक add_person अंत बिंदु के लिए चाहते हैं। यदि मैं PUT का उपयोग करता हूं, तो मुझे चित्र में सभी डेटा निर्दिष्ट करने की आवश्यकता है यदि मैं PATCH का उपयोग करता हूं, तो मुझे अभी भी तस्वीर में सभी लोगों को निर्दिष्ट करने की आवश्यकता है। बेशक मैं बस /api/picture/:id/add_people यूआरएल उत्पन्न कर सकता था और वहां मैं अपनी समस्या को संभालने में सक्षम था। इसके साथ समस्या यह है कि यह साफ महसूस नहीं करता है।

एक अन्य समाधान /api/picture/:id/people अंत बिंदु उत्पन्न करने के लिए किया जाएगा, और वहाँ मैं GET, POST, PUT कर सकता है, यह एक नई संसाधन है की तरह है, लेकिन मैं यह कैसे लागू करने के लिए पता नहीं है और यह नए लोगों को बनाने के लिए अजीब लगता है इस संसाधन के तहत।

कोई विचार?

+0

मैं किसी भी तरह से वही सवाल http://stackoverflow.com/questions/8613522/how-to-put-product-to-cart-via-tasytpie-api – seb

+1

क्षमा @seb मैं मेरी समस्या के लिए खोज के लिए कहा और मुझे आपको सवाल नहीं मिला। यदि आप चाहते हैं, तो मैं अपना प्रश्न हटा सकता हूं, लेकिन कृपया, अपना नाम बदल दें, क्योंकि "tasytpie API के माध्यम से कार्ट को उत्पाद कैसे डालें?" बस बहुत विशिष्ट –

+0

@seb है - आपका प्रश्न अभी भी खुला है, मुझे नहीं लगता कि आपने जवाब स्वीकार कर लिया है! – Mutant

उत्तर

4

मैंने एपीआई संसाधन के save_m2m फ़ंक्शन को ओवरराइड करके इसे कार्यान्वित किया। यहां आपके मॉडल का उपयोग करके एक उदाहरण दिया गया है।

def save_m2m(self, bundle): 
    for field_name, field_object in self.fields.items(): 
     if not getattr(field_object, 'is_m2m', False): 
      continue 

     if not field_object.attribute: 
      continue 

     if field_object.readonly: 
      continue 

     # Get the manager. 
     related_mngr = getattr(bundle.obj, field_object.attribute) 
      # This is code commented out from the original function 
      # that would clear out the existing related "Person" objects 
      #if hasattr(related_mngr, 'clear'): 
      # Clear it out, just to be safe. 
      #related_mngr.clear() 

     related_objs = [] 

     for related_bundle in bundle.data[field_name]: 
      # See if this person already exists in the database 
      try: 
       person = Person.objects.get(name=related_bundle.obj.name) 
      # If it doesn't exist, then save and use the object TastyPie 
      # has already prepared for creation 
      except Person.DoesNotExist: 
       person = related_bundle.obj 
       person.save() 

      related_objs.append(person) 

     related_mngr.add(*related_objs) 
संबंधित मुद्दे