Задать вопрос

Тел: +7 965 3737 888

580

Просмотров

4

Ответов

models with order (+admin editing)

Use this abstract model if you want to add "order" to a given model. Once you do, you will get automatic "up" and "down" links for each model row.

One problem is that if the user sorts by another row, the up and down links are still there, but now don't make a lot of sense.

Вопрос полезен? Да0/Нет0
file_2313.py(3.7Кб)
None

Ответы (4):

Ответareich:09.07.2009
Ответ полезен? Да0/Нет0

Thanks for this! I went looking for how to order user data in ModelAdmin and was led here. I agree w/andybak, this would be nice to be ajax-y. It would be really nice w/some up/down triangles too.

It looks like the metaclass needs to stay w/the models, unless you splay it out to another file, in which case you would need to also import it. Yes, the view is only for the admin. good luck w/your django site, mdgart!

Ответmdgart:12.06.2009
Ответ полезен? Да0/Нет0

I'm sorry for the silly question, but i'm a beginner: Where i should insert the metaclass code? And the view code is only for the admin, right? Again, sorry for the stupid question, but i wish to have this functionality on a website that i'm developing. Thanks

Ответslafs:09.03.2010
Ответ полезен? Да0/Нет0

that's a nice one! thanks!

Ответjeffreyatw:11.08.2009
Ответ полезен? Да0/Нет0

Thanks for the snippet! Found a bug in move_up() though...

With the code as is, whenever you try to move something up, it goes to the top of the list. This is because you're querying the database for the first element in the collection of elements whose order is less than the current element. Since we aren't ordering, the first element will always have order 0.

Change the lower_model assignment to this:

lower_model = ModelClass.objects.filter(order__lt=higher_model.order).order_by('-order')[0]

(note the order_by) and it should work properly.