Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

django - Tastypie, add element to a many to many relationship

I'm building a django tastypie api, and I have a problem with adding elements in ManyToMany relationships

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

resources:

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

My problem is that I would like to have an add_person end point in my picture resource. If I use PUT, then I need to specify all the data in the picture If I use PATCH, I still need to specify all the people in the picture. Of course I could simply generate the /api/picture/:id/add_people URL and there I could handle my problem. The problem with that is that it does not feel clean.

Another solution would be to generate the /api/picture/:id/people end point, and there I could do GET, POST, PUT, like it's a new resource, but I don't know how to implement this and it seems strange to create new people under this resource.

Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I implemented this by overriding the save_m2m function of the API Resource. Here is an example using your models.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...