2012-04-11 24 views
8
Bir Django tastypie API inşa ediyorum

bir sayıda eleman eklemek ve ben, models.py Tastypie, birçok ilişki

Örnek ManyToMany ilişkiler

öğeleri eklerken bir sorun var

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

kaynaklar:

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

sorunum benim resmim kaynakta bir add_person bitiş noktası istiyorum olmasıdır. PUT kullanırsam, resimdeki tüm verileri belirtmem gerekiyorsa PATCH kullanırsam, resimdeki tüm kişileri belirtmem gerekiyor. Tabii ki sadece /api/picture/:id/add_people URL'sini oluşturabilirim ve orada sorunumu çözebilirim. Sorun şu ki, kendini temiz hissetmiyor.

Başka bir çözüm /api/picture/:id/people bitiş noktasını oluşturmak için olurdu ve orada yeni bir kaynak olur diye, GET, POST, PUT yapabilirdi, ama bu nasıl uygulanacağı bilmiyorum ve yeni insanlarla oluşturmak için garip görünüyor bu kaynak altında.

Herhangi bir düşünce?

+0

ben bir şekilde aynı soruyu http://stackoverflow.com/questions/8613522/how-to-put-product-to-cart-via-tasytpie-api – seb

+1

Üzgünüm @seb Benim sorunum aramış istedi ve ben seni soru bulamadım. İsterseniz, sorumu silebilirim, ancak lütfen, "Tasytpie API'sı ile Ürünü Sepete Nasıl Verebiliriz?" sadece çok spesifik –

+0

@seb - sorunuz hala açık, yanıtı kabul ettiğinizi görmüyorum! – Mutant

cevap

4

Bunu, API Kaynağının save_m2m işlevini geçersiz kılarak uyguladı. Modellerinizi kullanarak bir örnek.

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) 
İlgili konular