Saturday, December 22, 2012

Re: Is their an easy way to implement an ordered list of, say, urls in Django

On Sat, 22 Dec 2012 17:22:05 -0800 (PST), Doug S <webcoach101@gmail.com>
declaimed the following in gmane.comp.python.django.user:


> I want to store an ordered list of items in my models
> and allow insertions and appending to the end of the list.
> I'm aware that often with ordered data the implementation is nice and clean
> where you store the items as a model and specify what field to sort them
> under when you query
> or with a Meta field in your model.
> I don't have a field to sort my items by. Of course I could create one,
> like a list of integer indexes.

By relational theory, a relation (table to the muggles) is
inherently unordered. Any order is imposed upon the data from the
outside (eg; sorting the data on some set of attributes when retrieving
it).

> But then when I insert an item I have to update all the items after it,
> doing a separate db query.

Presuming an integer "order" field, where you know, let's say, that
this record will be inserted at position #6, it requires performing one
SQL query to update the database, followed by one SQL insert for the new
record:

update theTable set
order = order + 1
where order >= theInsertNumber;
insert into theTable (order, ...)
values (theInsertNumber, ...);

which could (SHOULD) be done as one transaction.

This DOES imply that Django's ORM may not be suitable (unless the
new record insert operation can be over-ridden to use raw SQL rather
than the ORM). "order" should probably also be a unique (no duplicates)
index to catch any mis-applied inserts.

Appending a record requires obtaining the current maximum first

select max(order)+1 as newEntry from theTable;
insert into theTable (order, ...)
values (newEntry, ...);
,

> Probably a better way would be to implement a linked list so that insertion
> only needs to update
> the item before and after the inserted item.

But is not usable when doing multiple record selections, since you
can't sort by a link reference. You have to do a bunch of single-record
select queries, specifying the order (link) value of the previous record
as the where criteria of the new record.

> I could also load the whole list into memory while editing and then save it
> to the db once all editing was
> complete.

And updating ALL records one at a time is better than updating a
bunch of "order" fields in one query????

This proposal is equivalent to just dumping the database table and
creating it new on every addition of a record. If the data is not being
used in any join operations you might as well just save it as a CSV file
and reload that each time.


> I'm thinking this must be a pretty common thing to do even though many
> lists can be ordered nicely
> by something like date . . . I'm just wondering if there is a common easy
> way to do this in django
> that I haven't stumbled upon yet. Is there an app that is to linked lists
> like django-mptt is to trees?

Well, a linked list can be looked at as a binary tree where one limb
has been chopped off at each branch point. But (without looking at mptt)
I suspect that module is just internally maintaining an "order" field
and doing lots of self-join selects.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate