So after some research I found a way to have filtering by current language applied to all product access without forking Satchmo. My solution consists of monkeypatching the ProductManager class, adding a get_query_set method that applies filters as desired. I also patched the class with "use_for_related_fields = True" to force the usage of the modified manager when retrieving related objects (e.g., all products of a given category).
Just for the benefit of anyone trying to do something similar I'll point out this: my previous unsuccessful approach (see last post) was an attempt to replace the default manager in Product (the "objects" attribute) with a modified instance of ProductManager. Avoid that. I'd prefer not to resort to monkeypatching to achieve my goals but it's the only thing crossing my mind that works. Alternative suggestions are welcome. thanks, --lucio On Tue, Feb 8, 2011 at 7:52 PM, Lucio Aguirre <[email protected]>wrote: > Hi Marc, > > First of all thanks for your thoughts and apologies for the delayed > response (just wanted to explore Satchmo and Django a bit more before > getting back to you). > > My background goes somewhat like this: > - reasonably experienced with Python > - much less with Django > - brand new to Satchmo > > Hence I stepped back a bit to read relevant portions of Django's > documentation more carefully (Managers) and look at Satchmo's models module. > > [comments inline] > > thanks > --lucio > > > On Mon, Feb 7, 2011 at 3:13 AM, Dierckx, Marc < > [email protected]> wrote: > >> Hello, >> >> I had a similar issue: products only available in certain countries. >> >> My solution : >> >> 1 Extend the product model as described in the manual with a >> ManyToManyField to languages. >> > 2 write a template context processor to add language information of the >> user (either browser language or language of the userprofile if logged >> into the template context (I do both country and language) >> 3 change the category.html to display/ not display certain products >> >> Unless I am missing something in your approach, it doesn't seem to be what > I'm looking for. I'm OK with your step (1); but as for (2) and (3), I'd > rather solve the problem at the model level (thru custom managers, more on > this below) than at the template level. > > Just to make sure I'm being clear, when a user selects a language I'd like > him/her to see available products only for that language in *all* pages of > the store. In your solution, the quick-order page, for instance, will not > be filtered by the user's selected language. > > So I tried a solution in the following lines: > (a)- create an attribute-option "available"; then in the Products admin > page, I add the attribute for each language the product is available. This > is similar to your step 1 but eliminates the need to create a product > extension (by abusing the existing ProductAttribute model with its out of > the box multillingual support). > (b)- create a Product manager that filters Products by selected language. > > I haven't tested this extensively but from my limited experimentation it > seems to be working. The tricky bit I still don't have a satisfactory > solution is how to hook the new language-aware Product manager in the > Product model. The only thing that worked is modifying Satchmo's codebase > directly (I know, this is a no-no) adding a get_query_set() override to > ProductManager that returns a queryset that filters by current language. > > I tried several other things: subclassing ProductManager, various > monkeypatchings on the Product class (to inject a new "objects" manager > attribute), etc, but I could not manage to get it working. Unfortunately > all the metaprogramming going on in Django's ORM seems to break familiar OOP > semantics of the created objects. Anyhow, it'd be great if someone with > more Django/Satchmo experience could throw me a bone. > > alternatively you can of course implement step 2 and 3 also in >> Javascript. >> >> I would also love to have feedback whether this approach is good django >> programming practice, but in any case it works. Code is available but I >> do not guarantee state-of-the-art python is I restarted programming 1/2 >> year ago with a background of intensive 8080 assembler coding (for the >> ones that still remember computer medieval). >> >> > >> regards, >> >> marc >> >> >> -----Original Message----- >> From: [email protected] >> [mailto:[email protected]] On Behalf Of Lucio >> Sent: Sonntag, 6. Februar 2011 03:56 >> To: Satchmo users >> Subject: Seeking advice on how to model titles (products) in >> multilingual bookstore >> >> Hi all, >> >> I'm trying to implement a small multilingual web bookstore using >> Satchmo where titles fall in one of the following classes: >> (1)- title is only available in the store's default language >> (2)- title is only available in a language different from store's >> default >> (3)- title is available in both store's default *and* another language >> (i.e. there is a translation published for that title). >> >> As for functional requirements, I'd like a user to see only what is >> available in his/her selected language, e.g., >> - when the store's default language is selected, user should see >> titles in (1) + (3); >> - when other language is selected, user should see titles in (2) + (3) >> for the selected language. >> >> The approach that first comes to mind is to create each title in the >> store's default language and enter appropriate translations in the >> "Product Translations" section of the Product Admin page. But for >> titles in (3), this modeling seems to imply that there is a single >> product which is just being described in multiple languages. This >> does not appear to be the case as each title/language combination will >> most likely have different properties (number of pages, isbn, >> publishers, etc) and thus defines a product in its own right. >> Furthermore this approach doesn't seem to be appropriate for titles in >> (2) where no product should exist for the store's default language, >> just for the translation. >> >> Another approach could be create each title/language as a separate >> product. For example, if the bookstore sells "Hamlet" in English, >> French and German we'd have 3 different products in the catalog. >> While this data model seems to reflect reality more accurately, it is >> still not clear to me how I could implement the requirement of only >> showing the user titles available in the selected language. >> >> Is there a recommended way to customize Satchmo to handle what I am >> trying to implement? >> Any advice would be greatly appreciated! >> >> thanks, >> --lucio >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Satchmo users" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/satchmo-users?hl=en. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Satchmo users" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/satchmo-users?hl=en. >> >> > -- You received this message because you are subscribed to the Google Groups "Satchmo users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/satchmo-users?hl=en.
