acts_as_list?

26 views
Skip to first unread message

Paul Barry

unread,
Sep 28, 2006, 9:00:21 PM9/28/06
to django...@googlegroups.com
Does django has something like the rails ActiveRecord acts_as_list?

For those who don't know what acts_as_list is, it gives you the api
for dealing with an ordered list of objects. For example, think if
was modeling a book. I would have a Book object with a one to of a
simple Netflix queue. You might have a database table like this:

class Book(models.Model):
title = models.CharField(maxlength=50)

class Chapter(models.Model):
book = models.ForeignKey(Book)
position = models.IntegerField
title = models.CharField(maxlength=50)

Now, it would be nice if I could do something like this:

#Create the book
book = Book(title="Dive Into Python")

#Create a chapter, have it automatically set the position
#based on the current number of chapters
chapter = Chapter(book=b, title="Installing Python")

#If that's not possible, how would you do it manually, like this?
chapter = Chapter(book=b, title="Installing Python",
position=book.chapter_set.count()+1)

#Anyway, save the chapter
chapter.save()

#Make another chapterm. By the way, I'm a Java programmer, new to Python,
#will this work like I think? Does chapter now point to a new
instance of Chapter,
#and changes after this line would have no effect
#on the chapter I created in the previous line?
chapter = Chapter(book=b, title="Your First Python Program",
position=book.chapter_set.count()+1)

#Now I realize the chapters are out of order, so I want to re-order them
#It would be nice if this method would set the value of the current
#chapter's position to 1, and the value of the other chapter's position to 2
chapter.move_lower

Here are some links to acts_as_list:
http://rubyonrails.org/api/classes/ActiveRecord/Acts/List/InstanceMethods.html
http://brianfox.wordpress.com/2006/08/08/acts_as_list-in-ruby-on-rails/

Here are the methods with a description:
decrement_position Moves an item position down one
first? Returns true if item is first in list,
false if not higher_item Returns the record immediately above the
current record
in_list? Returns true if item is in list, false if not
increment_position Moves an item up one position
insert_at(position = 1) Adds an item to the list in a specific position
last? Returns true if item is last in list,
false if not
lower_item Returns the record immediately below the
current record
move_higher Moves an item up one position
move_lower Moves an item down one position
move_to_bottom Moves an item to the bottom of the list
move_to_top Moves an item to the top of the list
remove_from_list Removes an item from the list

Holger Schurig

unread,
Sep 29, 2006, 2:25:52 AM9/29/06
to django...@googlegroups.com
> #Create a chapter, have it automatically set the position

You may amend the model's save() method to do that for you:

def save(self):
# Fiddle with the positon field
self.position = <whatever suits you>
# And now the actual save operation
super(Item, self).save()

> move_higher Moves an item up one position
> move_lower Moves an item down one position
> move_to_bottom Moves an item to the bottom of the list
> move_to_top Moves an item to the top of the list

For them, you'd need to have an operation that changes the
position of this chapter an probably of all the other chapters.
You could create them as methods of the model and then manually
call them:

def move_higher():
# find previous chapter
# swap position of previous and this chapter

def move_lower():
# find next chapter
# swap position of next and this chapter

def move_to_bottom():
# find last chapter
# position = lastchapter.position+1

def move_to_top():
# find first chapter
# if first.position>0:
# position = first.position-1
# else:
# tediously renumber most if not not all chapters

> remove_from_list Removes an item from the list

chapter.delete()

Reply all
Reply to author
Forward
0 new messages