2012-04-11 14 views
8

Sto costruendo una Django api tastypie, e ho un problema con l'aggiunta di elementi in ManyToMany rapportiTastypie, aggiungere elementi a una relazione molti a molti

Esempio, models.py

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", 
    ) 

risorse:

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", 
    ) 

mio problema è che mi piacerebbe avere un punto add_person fine a se la mia risorsa immagine. Se uso PUT, allora ho bisogno di specificare tutti i dati nell'immagine Se uso PATCH, ho ancora bisogno di specificare tutte le persone nella foto. Ovviamente potrei semplicemente generare l'URL /api/picture/:id/add_people e lì potrei gestire il mio problema. Il problema è che non sembra pulito.

Un'altra soluzione potrebbe essere quella di generare il punto finale /api/picture/:id/people, e lì ho potuto fare GET, POST, PUT, come se fosse una nuova risorsa, ma non so come implementare questo e sembra strano per creare nuove persone sotto questa risorsa.

Qualche idea?

+0

ho chiesto in qualche modo la stessa domanda http://stackoverflow.com/questions/8613522/how-to-put-product-to-cart-via-tasytpie-api – seb

+1

@seb dispiace ho cercato il mio problema e non ti ho trovato domanda. Se lo desideri, posso eliminare la mia domanda, ma per favore, cambia il nome del tuo, dal momento che "Come inserire il prodotto nel carrello tramite l'API di tasytpie?" è troppo specifico –

+0

@seb - la tua domanda è ancora aperta, non vedo che hai accettato risposta! – Mutant

risposta

4

L'ho implementato sovrascrivendo la funzione save_m2m della risorsa API. Ecco un esempio usando i tuoi modelli.

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) 
Problemi correlati