On Friday, 4 October 2013 07:45:40 UTC+1, Marc'h wrote:

> Hello,
>
> I'm battling with Django in order to try avoiding object persistence when 
> initialising foreign key model attributes. The app I'm using Django for 
> implements a facade for an already existing database. It's main purpose is 
> to be used as a (read-only) ReST server returning objects built by 
> aggregating attributes from the already existing database. The Django app 
> itself has model classes (and associations) like any other Django app. 
> Renderers and Serializers based on those supplied by the rest_framework 
> modules have been written and tested. We started by configuring the Django 
> app to use a SQLite file, as we're don't really care about object 
> persistence. However, when building model instances (by 
> extracting/aggregating data from the original non-Django managed database), 
> we hit a severe perfomance bottleneck. Each time we initialize a model 
> attribute which is a collection of related instances, all these are 
> apparently persisted to the Django database.
>
> For example, with a classical Order/Items association, we'd have :
>
> 01: orderattributes={...extract attributes from external database using 
>> SQL...}
>> 02: order=Order(**orderattributes)
>> 03: orderitems=[]
>> 04: for item in ...items belonging to order...
>> 05:    itemattributes={...extract attributes from external database using 
>> SQL...}
>> 06:    item=Item(**itemattributes)
>> 07:    orderitems.append(item)
>> 08: order.items_set=orderitems
>
>
> When reaching line 8, it seems that every Item instance of the orderitems 
> collection is persisted in the Django database, which considerably slows 
> down the application.
> I tried to use an in-memory SQLite database, but couldn't make it work, as 
> the syntax for declaring a shared in-memory database (file::memory:...) is 
> not handled correctly, yielding a file-based database instead.
>
> Thus, I'd like to know if there is any means to by-pass the automatic 
> persistence mechanism when using collection attributes which are 
> foreign-key based.
>
> Thanks for any pointers !
>
>
> Marc'h
>
>
You have some fairly severe misunderstandings here. Django does not have 
any "automatic persistence mechanism". The *only* time a model instance is 
persisted to the database is when you call `save()` on it. I don't know 
what `order.items_set` is, but even assuming that it's a reverse foreign 
key manager, Django has no functionality that will automatically save those 
items - especially as the parent Order has not been saved, so therefore has 
no primary key for those FKs to link to. 

Part of the trouble here is that the code you've posted here (apart from 
being on a grey background, which makes it very hard to read) isn't your 
actual real code. You should post the actual code, and indicate where you 
think this "automatic persistence" is happening, then we can help you work 
out what really is going on.
--
DR.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/83eef91d-0158-4610-bc8e-c7e7f18918b5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to