Figuring out how to implement user-defined product sort order in Cartridge

76 views
Skip to first unread message

Sam W

unread,
Apr 7, 2016, 11:51:36 PM4/7/16
to Mezzanine Users
So I end up doing a lot of weird stuff with Cartridge, but something a bit more normal has been plaguing me recently; user-defined sort orders for products in Cartridge's categories.

Basically, clients' sales staff want to be able to manually drag and drop products in categories to change their initial order when the page loads. At the moment, they're doing all sorts of stuff to achieve this, including unloading and reloading products so that "recently added" does what they want. D:

Obviously this is not a normal feature in Cartridge for a bunch of reasons, but I was wondering if anyone else had run into this before, and if so how you worked around it.

As I'm already customising cartridge, that's the approach I'll likely end up taking. It seems like the ideal way to do it is to make products orderable with respect to categories and then find a way to add a product-order widget to the category page in the admin that will update the category-respective order of products when edited. The latter part is the most difficult part of the implementation for me, as adding custom widgets to the grappelli is not exactly my strong suite. Does anyone have any advice on that subject?

Danny

unread,
Apr 8, 2016, 12:24:48 AM4/8/16
to Mezzanine Users
I've customised cartridge as well, and one of the things I did was add an 'ordering' field to the model (I did at first try subclassing Orderable but it wasn't successful). Then in the Products Admin, I've allowed the ordering field to be visible/editable so an admin can numerically change the order of the products. This isn't per category, it's overall, but it's enough for me.

In the category template, I've removed the sort dropdown, so the only order that products appear in is the default (which I've set to be based on 'ordering') and that way the products appear in the order I want them.

It's a simple and crude solution, but it's enough for me.

I think if you want drag-and-drop ordering you may want to *start* by making the Product class subclass Orderable, but to provide that drag-drop feature, you'll have to adapt the TabularDynamicInline javascript behaviour to apply to a top level model (rather than an inline). I could never get the drag/drop ordering to work on any top-level model, so I just gave up and decided to use numeric ordering as it was simpler for me.

Seeya. Danny.


 

Eduardo Rivas

unread,
Apr 8, 2016, 12:27:18 AM4/8/16
to mezzani...@googlegroups.com
I've used Django admin sortable
(https://github.com/iambrandontaylor/django-admin-sortable) to add
drag-n-drop ordering to models that are not inlines. It adds a new
button to the list display where you can do the sorting.

Sam

unread,
Jun 9, 2016, 12:53:05 AM6/9/16
to mezzani...@googlegroups.com
Thanks for the help, guys. Sorry about taking a long time to respond, I got pulled onto some other tasks.

They wanted to order the products per-category, so I ended up using django-sortedm2m and migrating the product-category relation to a sorted m2m. I then had to modify the page processor so it would accept "'shop_product_categories.sort_value'" as a sort order. All that was needed for that was to use a queryset based on page.category.products. I did it a slow way that basically just used id__in to filter based on the already computed list. My guess is you could do it properly by making a version of ProductsManager.published that returns a Q. (Unless there's a more direct way to filter one qs with another? Idk, I'm no DjangoMaster, I'd rather be writing SDL.) In my case, this is fairly feasible as I'm already modifying it to do per-user-per-page permissions, so I could just refactor my published method into a part that makes a Q and a part that applies it.

Anyhow, doing it this way gives you a sort order that is different per-category. It was a bit brainhurt to actually cobble together, but it does the thing. What I learned is that with custom m2ms, where you start the queryset has effects on what you can query by. Or in other words, related managers exist. I guess I knew that, but not what it meant for querying.

On 8 April 2016 at 16:27, Eduardo Rivas <jeriva...@gmail.com> wrote:
I've used Django admin sortable (https://github.com/iambrandontaylor/django-admin-sortable) to add drag-n-drop ordering to models that are not inlines. It adds a new button to the list display where you can do the sorting.


--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/vZCr4K6Qk4M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages