lluisu
lluisu

Reputation: 487

Create objects from different models in one handler in Piston Django

I'm using Piston to create an API for an application in Django.

I'll try to explain my doubt on an easy way. Let's think I've got two models:

class Device(models.Model):
    id = models.TextField(...)

class Person(models.Model):
    name = models.TextField(...)
    device = models.ForeigKey(Device)

Now, if I receive an url like this:

(r'^api/(?P<person_name>\w+)/(?P<device_id>\w+)$',handler),

I want to add a person to the DB and, to do that, I need to add a new Device to the DB, but, since handlers in Piston are linked to a Model, how can I add a device to the DB in the same handler?

I tried something like this:

class PersonHandler(BaseHandler):
    allowed_methods= ('PUT')
    model = Person

    def create(self, request, person_name, device_id):
        Device.objects.create(id=device_id)
        d = Device.objets.get(id=device_id)
        Person.objects.create(name=person_name,device=d)
        return rc.CREATED

But I guess it won't work.

How can I do what I want to do?

Upvotes: 0

Views: 94

Answers (0)

Related Questions