Tastypie,在许多添加元素一对多的关系(Tastypie, add element to a

2019-06-23 13:44发布

我要建一个Django tastypie API,和我有一个问题,在添加元素ManyToMany关系

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

资源:

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 URL有我能胜任我的问题。 但问题是,它不觉得干净。

另一个解决方案是生成/api/picture/:id/people的终点,在那里我可以做的GETPOSTPUT ,就像是一个新的资源,但我不知道如何实现这一点,它似乎有些奇怪创建这个资源下,新人们。

有什么想法吗?

Answer 1:

我通过重写API资源的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)


文章来源: Tastypie, add element to a many to many relationship