REST API and writing to the database

8 views
Skip to first unread message

Huuuze

unread,
Jul 29, 2008, 4:05:16 PM7/29/08
to Django REST interface
I'm in the process of using the Django REST Interface (excellent work,
btw) and I'd like to force it to only allow POSTs. With that being
said, here is my Collection:

person_resource = Collection(queryset = Person.objects.all(),
permitted_methods = ('POST'),
receiver = XMLReceiver(),
responder=XMLResponder())

Since this is new territory for me, I have a question:

I am expecting the client to send me an XML string. I'm guessing that
I need to use a non-model-based resource so I can interpret the
information that is being sent and save it to the database
accordingly. Is that correct?

If so, can someone provide me with an example on how to use
XMLReceiver? I've reviewed the examples in the Wiki, but all of them
deal with reading data, not writing data.

Alaa Salman

unread,
Jul 29, 2008, 4:17:28 PM7/29/08
to django-res...@googlegroups.com
Hi,

First off, you need to correct permitted_methods to read
permitted_methods = ('POST',)
So its a single element list.(notice the trailing comma)

If what you want to do is store persons into the database and person is
a model, then no you don't need a non-model resource.

The concept is very simple...just POST data to the url that maps to this
resource and django-rest-interface will deserialize it and store it into
the database. To look at the structure of the needed xml, add GET as a
permitted method and visit the url and look at the generated xml and
just replicate that.

--
Regards,
Alaa Salman
Entrepreneur, Contractor, Programmer
http://www.codedemigod.com
Free Software Foundation Associate Member #6304

"One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man." ---Elbert Hubbard
"Never measure the height of a mountain until you have reached the top. Then you will see how low it was." ---Dag Hammarskjold

Huuuze

unread,
Jul 29, 2008, 4:31:53 PM7/29/08
to Django REST interface
Thank you for the quick response.

Person is a model in my app. However, I don't have control over the
XML format that's coming from the client. The users of the client app
have an XML schema that I must follow.

I'm guessing I'll need to parse that myself and then do the insert,
correct?
> Entrepreneur, Contractor, Programmerhttp://www.codedemigod.com

Huuuze

unread,
Jul 29, 2008, 4:38:03 PM7/29/08
to Django REST interface
One side question. To test my POST, I created a dummy HTML document
that POSTs a form:

<html>
<body>
<form method="POST" action="http://127.0.0.1:8000/ws/person/">
<input type="hidden" name="person" value="<?xml version='1.0'
encoding='utf-8'?><django-objects version='1.0'></django-objects>">
<input type="submit" value="GO!">
</form>
</body>
</html>

Unfortunately, I consistently receive the following error:

SAXParseException at /ws/person/
<unknown>:1:5: not well-formed (invalid token)

Any thoughts on what's causing the problem?


On Jul 29, 4:17 pm, Alaa Salman <alaasal...@gmail.com> wrote:
> Entrepreneur, Contractor, Programmerhttp://www.codedemigod.com

Alaa Salman

unread,
Jul 29, 2008, 4:38:38 PM7/29/08
to django-res...@googlegroups.com
Huuuze wrote:
> Person is a model in my app. However, I don't have control over the
> XML format that's coming from the client. The users of the client app
> have an XML schema that I must follow.
>
> I'm guessing I'll need to parse that myself and then do the insert,
> correct?
>

Ah i see. Well, i think you can try and subclass Receiver and implement
a specialized XMLReceiver. But take not that the default XMLReceiver
uses the django serializer, so it expects the xml in a certain way. This
is where you'll have to use an xml parser and pass back a dictionary.
Take a look at the code in receiver.py for inspiration.

Alaa Salman

unread,
Jul 29, 2008, 4:49:33 PM7/29/08
to django-res...@googlegroups.com
Huuuze wrote:
> One side question. To test my POST, I created a dummy HTML document
> that POSTs a form:
>
> <html>
> <body>
> <form method="POST" action="http://127.0.0.1:8000/ws/person/">
> <input type="hidden" name="person" value="<?xml version='1.0'
> encoding='utf-8'?><django-objects version='1.0'></django-objects>">
> <input type="submit" value="GO!">
> </form>
> </body>
> </html>
>
> Unfortunately, I consistently receive the following error:
>
> SAXParseException at /ws/person/
> <unknown>:1:5: not well-formed (invalid token)
>
> Any thoughts on what's causing the problem?
>
I believe the problem here is caused because you're using a form to POST
xml. If you want to POST pure xml, try using python's urllib.

Huuuze

unread,
Jul 29, 2008, 5:06:38 PM7/29/08
to Django REST interface
One last question (for today!) ;).

One of my Model's fields is a ForeignKey. When performing a GET, the
XML returns the following...

<field to="person.book" name="book" rel="ManyToOneRel">1</field>

...but what I really want is the Model details from that FK'ed field.
In this example, let's say that I want to return the First and Last
name of the Person and the names of the Books he's read.

Is that possible using django-rest-interface?

On Jul 29, 4:49 pm, Alaa Salman <alaasal...@gmail.com> wrote:
> Huuuze wrote:
> > One side question.  To test my POST, I created a dummy HTML document
> > that POSTs a form:
>
> > <html>
> > <body>
> >    <form method="POST" action="http://127.0.0.1:8000/ws/person/">
> >            <input type="hidden" name="person" value="<?xml version='1.0'
> > encoding='utf-8'?><django-objects version='1.0'></django-objects>">
> >            <input type="submit" value="GO!">
> >    </form>
> > </body>
> > </html>
>
> > Unfortunately, I consistently receive the following error:
>
> > SAXParseException at /ws/person/
> > <unknown>:1:5: not well-formed (invalid token)
>
> > Any thoughts on what's causing the problem?
>
> I believe the problem here is caused because you're using a form to POST
> xml. If you want to POST pure xml, try using python's urllib.
>
> --
> Regards,
> Alaa Salman
> Entrepreneur, Contractor, Programmerhttp://www.codedemigod.com

Alaa Salman

unread,
Jul 29, 2008, 5:28:45 PM7/29/08
to django-res...@googlegroups.com
Huuuze wrote:
> One last question (for today!) ;).
>
> One of my Model's fields is a ForeignKey. When performing a GET, the
> XML returns the following...
>
> <field to="person.book" name="book" rel="ManyToOneRel">1</field>
>
> ...but what I really want is the Model details from that FK'ed field.
> In this example, let's say that I want to return the First and Last
> name of the Person and the names of the Books he's read.
>
> Is that possible using django-rest-interface?
>

Well, this really has nothing to do with django-rest-interface. It has
to do with django itself. The django serializer and deserializer is
what's doing the work here. The good news is that you can get a model
instance from the deserializer....

This conflicts with your previous emails, since you asked about
receiving xml rather than providing it. But regardless, try changing the
queryset provided to the collection. Since all the app does here is
serialize the given queryset.

Reply all
Reply to author
Forward
0 new messages