Manually sorting items in a table - Doubly-Linked List vs sort_index
I've got items in a list that a user can arbitrarily move around in whatever order they want. Additionally, they can add a group of items to a list in a specific position within that list. Further, they can sort multiple items at once, meaning they can select one to many, sequential or non-sequential, and move them as a group to a new position in the list. The items will always be moved in order from top to bottom, so if you select multiple items, whether they're sequential or non, they will be flattened together into a group sequentially from the top to bottom. For example: [1, 2, 3, 4, 5] If I select 1, 4, and 5 and move them to where id 1 in the list is, it will look like this: [1, 4, 5, 2, 3] I need to refer to items not by their indices, but by their ids. So, it's possible that a list might look like this (id-wise) [27, 31, 45, 7, 21] When I want to move items, I pass a before_id to determine which item they should move before. If I pass 0 for before_id, they are moved to the end of the list. I've got two possible choices here: 1) Doubly-linked list Each item in the list has a next_id and prev_id, which are foreign keys to the Model that those objects are. When I move items in the list, I calculate and modify the affected rows next/prev id fields. 2) sort_index A more brute force method is to have each item in the list have a sort_index, and when I add/move items to the list, I determine where they're supposed to go and calculate the sort_index for every row in the table. This actually isn't as easy as it seems because you can't Array.insert on a QuerySet. I'm leaning towards a doubly-linked list. Does anyone have any advice or other, more efficient ways of accomplishing this? Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Manually sorting items in a table - Doubly-Linked List vs sort_index
While I appreciate your response, your assumption is incorrect. However, I've found the solution and it's rather easy. The List Delete function closes gaps by setting an item.prev = item.next and item.prev = item.next The List Insert function injects item(s) either at the end of the list (when the first item inserted's prev is the tail, and the tail's next is set to the first item inserted), or in the middle of the list (using similar logic). The List Move function simply calls List Delete to close the gaps, and List Insert with the new position. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Manually sorting items in a table - Doubly-Linked List vs sort_index
Sorry I accidentally repeated myself. List Delete closes gaps by setting item.prev = item.next and item.next = item.prev. On Oct 8, 5:03 am, Steven Sacks wrote: > While I appreciate your response, your assumption is incorrect. > However, I've found the solution and it's rather easy. > > The List Delete function closes gaps by setting an item.prev = > item.next and item.prev = item.next > The List Insert function injects item(s) either at the end of the list > (when the first item inserted's prev is the tail, and the tail's next > is set to the first item inserted), or in the middle of the list > (using similar logic). > The List Move function simply calls List Delete to close the gaps, and > List Insert with the new position. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Need help with this basic query (annotate vs aggregate)
Here is my model: class PlaylistTag(models.Model): playlist = models.ForeignKey(Playlist) tag = models.CharField(max_length=128) tag_count = models.PositiveIntegerField(default=1) The way this model works is simple. If you add a tag to a playlist, and the tag doesn't exist, the tag_count is 1. If you add the same tag again to that playlist, the tag_count is incremented. If you remove that tag, it is decremented. If it reaches 0, I remove the row entirely. A playlist can have multiple tags and a tag can belong to different playlists. What I want to do is wildcard search for a tag sorted by tag count descending, but instead of returning duplicate playlists, I want to sum the tag_count for each playlist that the search returns. For example: Playlist 1, "foo", 3 Playlist 1, "foobar", 2 A query for "foo" would return both of those rows, but I want to sum them for that playlist so that playlist has a "score" of 5 for that query and only return that playlist once in the querySet. querySet = PlaylistTag.objects.select_related().filter(tag__icontains = query).annotate(score=Sum("tag_count")).order_by("-score") The missing part that I don't understand, is how do I only sum tag_count for distinct playlists that are returned for that query. In plain English, I am searching the PlaylistTag table by tag, and I want to rank the results by which Playlist is most relevant for that search by how many tags matched the query. Does this make sense? -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Need help with this basic query (annotate vs aggregate)
If it helps, this is, I believe, somewhat equivalent SQL: SELECT playlist_id, tag, SUM(tag_count) score FROM playlist_tag WHERE tag like '%query%' GROUP BY playlist_id ORDER BY score DESC -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Need help with this basic query (annotate vs aggregate)
Also, I need to get back Playlist records (hence the select_releated()). -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.