Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small gotcha with snippets #836

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/how-to/tips.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Snippets and Synchronization
Imagine you have a page with related tags added via snippets, and you want to query all pages with a specific tag.

When synchronous translation is enabled, the tag will refer to the default language. However, if the user turns off
synchronous translation, the tag will refer to the page’s language. This could mean the user references a translated tag
within the translated page, causing the query to miss some pages tagged with the original tag.

To solve this, you can query both the default language and the current language, then filter based on the active
language.

```python
def get_something(request, tag):
lang = request.LANGUAGE_CODE
default_tag = tag.get_translation(tag.get_default_locale())
pages = SomePage.objects.live().filter(
Q(locale__language_code=lang),
Q(tags=default_tag) | Q(tags=tag)
).distinct()
```