Re: Best way to export data from Django?

225 views
Skip to first unread message

Kurtis Mullins

unread,
Aug 17, 2012, 6:35:35 PM8/17/12
to django...@googlegroups.com
Just curious, which data do you not have available when using signals? You mentioned the ID but this *should* be available during post_save and pre_delete hooks, if I'm not mistaken.
Also, I'd recommend caution on performing external server calls during a single HTTP Request. You might have better luck queuing this up with something like http://pypi.python.org/pypi/django-celery/

On Fri, Aug 17, 2012 at 12:41 PM, Tom <t...@distracts.co.uk> wrote:
Hi all,

I'm new to Django, but really enjoying using it so far!

What I'm trying to do is to export data from an admin interface, when a record is created or updated. I'm calling another web service to tell it the data on site [A] has changed, by using signals.

e.g.

@receiver(post_save, sender=Poll)
def notify_central_hub(sender, instance, created, **kwargs):
    if created:

        #make a string to hold the url of the request
        url = "http://www.example.com/webservice"

        #place POST data in a dictionary
        post_data_dictionary = kwargs

        #encode the POST data to be sent in a URL
        post_data_encoded = urllib.urlencode(post_data_dictionary)

        #make a request object to hold the POST data and the URL
        request_object = urllib2.Request(url, post_data_encoded)

        #make the request using the request object as an argument, store response in a variable
        response = urllib2.urlopen(request_object)


I'm able to export the data this way, but I can't get the attributes of the full model as I would as if I was using the Django shell. Particularly important is the ID of an object, because without that I won't know which ID to update on the remote web service. That's why I'm only dealing with "created" objects for now.

Could someone please advise me whether I'm going about this the right way, or if there is actually a better way of doing this? I looked into Django middleware but this appears to be more for changing the output at the view layer, not the data layer. But if there is a better way of doing this, I'd be keen to find out!

Many thanks,

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/zystPhngbI0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Melvyn Sopacua

unread,
Aug 17, 2012, 7:15:58 PM8/17/12
to django...@googlegroups.com
On 17-8-2012 18:41, Tom wrote:

> I'm able to export the data this way, but I can't get the attributes
> of the full model as I would as if I was using the Django shell.
> Particularly important is the ID of an object, because without that I
> won't know which ID to update on the remote web service. That's why
> I'm only dealing with "created" objects for now.

Why not? You have full access to the created instance with the instance
argument passed to the post_save signal.
You could for example use:
from django.forms import model_to_dict
data = model_to_dict(instance)
print data

and you should see the pk there.

--
Melvyn Sopacua

Tom

unread,
Aug 20, 2012, 8:17:05 AM8/20/12
to django...@googlegroups.com
Hi Kurtis

Perhaps I'm doing something wrong – I am looking at the dump in the console (I have debug mode turned on) and in the post data I'm only seeing the following

csrfmiddlewaretoken
u'pPjBtpAXdODSvuEYjGPNok5WaGDvxQp4'
pub_date_0
u'2012-08-17'
question
u'2011-01-01'
pub_date_1
u'12:00:00'
_save
u'Save'

I'm able to access all this data from my remote server but have no idea how to get the ID! The ID appears to be in the URL, i.e. admin/myapp/poll/6/

Django Celery looks like just the thing that I was hoping would exist! As you say, doing this in a single HTTP Request might cause problems.

Many thanks,

Tom

Tom

unread,
Aug 20, 2012, 8:22:02 AM8/20/12
to django...@googlegroups.com
Thanks Melvyn. Using model_to_dict I can now see the ID.

Tom
Reply all
Reply to author
Forward
0 new messages