How to load an object created from a model outside the project ?

26 views
Skip to first unread message

Matthieu

unread,
May 13, 2015, 10:01:45 AM5/13/15
to django...@googlegroups.com
Hi,

I saved with pickle an object created from a Django model :

class Country(models.Model):
    code
= models.IntegerField(primary_key=True)
    name
= models.CharField(max_length=30)
   
def __str__(self):
       
return (self.name)

england
= Country(code=1, name="England")
pickle
.dump(england, open( "england.p", "wb" ))

And I'm trying to access it outside Django, directly with Python :
england = pickle.load(open( "england.p", "rb" ))

But if fails :
ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

My question is : how can i read this object (and access to its attributes) with Python without Django ?

Thanks for your help,
Matthieu

Gergely Polonkai

unread,
May 13, 2015, 11:26:40 AM5/13/15
to django...@googlegroups.com

Hello,

if you don't have to modify the object, I would suggest using serialization instead of pickling.

Best,
Gergely

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec42d8bc-cecf-4583-b808-6832fa30706b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthieu

unread,
May 14, 2015, 6:06:44 AM5/14/15
to django...@googlegroups.com
Hi Gergely,

Thanks for you answer. 
I don't need to modify the object. I also tried with "dill" package but it's not working either. How can I use serialization with my example ?

Gergely Polonkai

unread,
May 14, 2015, 7:44:15 AM5/14/15
to django...@googlegroups.com
Hello,

depends on what exactly you want to do. You can take a look at Django’s own serialization solution[1], or, if you want to build a REST framework based on this, you may want to look at [2] or [3].

Best,
Gergely


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Matthieu

unread,
May 14, 2015, 4:44:55 PM5/14/15
to django...@googlegroups.com
These solutions seem interesting :) Thanks !
Reply all
Reply to author
Forward
0 new messages