2012-04-11 35 views
8

Tôi đang xây dựng một django api tastypie, và tôi có một vấn đề với việc thêm các yếu tố trong mối quan hệ ManyToManyTastypie, thêm yếu tố để một nhiều mối quan hệ

Ví dụ, 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", 
    ) 

tài nguyên:

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

Vấn đề của tôi là tôi muốn có một điểm cuối add_person trong tài nguyên hình ảnh của mình. Nếu tôi sử dụng PUT, thì tôi cần phải chỉ định tất cả dữ liệu trong hình Nếu tôi sử dụng PATCH, tôi vẫn cần chỉ định tất cả những người trong ảnh. Tất nhiên tôi có thể chỉ cần tạo URL /api/picture/:id/add_people và ở đó tôi có thể xử lý sự cố của mình. Vấn đề với điều đó là nó không cảm thấy sạch sẽ.

giải pháp sẽ được để tạo ra điểm cuối /api/picture/:id/people, và ở đó tôi có thể làm GET, POST, PUT, giống như đó là một tài nguyên mới, nhưng tôi không biết làm thế nào để thực hiện điều này và nó có vẻ kỳ lạ để tạo ra những người mới dưới tài nguyên này.

Mọi suy nghĩ?

+0

Tôi hỏi bằng cách nào đó cùng một câu hỏi http://stackoverflow.com/questions/8613522/how-to-put-product-to-cart-via-tasytpie-api – seb

+1

@seb Xin lỗi tôi đã tìm kiếm cho vấn đề của tôi và tôi không tìm thấy bạn câu hỏi. Nếu bạn muốn, tôi có thể xóa câu hỏi của mình, nhưng vui lòng thay đổi tên của bạn, vì "Cách đặt Sản phẩm vào giỏ hàng qua API tasytpie?" chỉ là quá cụ thể –

+0

@seb - câu hỏi của bạn vẫn mở, tôi không thấy bạn đã chấp nhận câu trả lời! – Mutant

Trả lời

4

Tôi đã thực hiện điều này bằng cách ghi đè hàm save_m2m của Tài nguyên API. Dưới đây là một ví dụ sử dụng các mô hình của bạn.

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) 
Các vấn đề liên quan