Skip to content

Commit

Permalink
tips page for handeling snippets and synchronization on or off.
Browse files Browse the repository at this point in the history
Added a better code example
  • Loading branch information
onno-timmerman committed Oct 31, 2024
1 parent 7618cf3 commit e625887
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions docs/how-to/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,33 @@ To solve this, you can query both the default language and the current language,
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()
from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from wagtail.snippets.models import register_snippet
from wagtail.models import TranslatableMixin, Page


@register_snippet
class Tag(TranslatableMixin, models.Model):
tag = models.CharField(_("Tag"), max_length=255)

def __str__(self):
return self.tag

class BlogPage(Page):
my_tag = models.ForeignKey(
"snippets.Tag",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)

def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
if self.my_tag:
default_tag = self.my_tag.get_translation(self.my_tag.get_default_locale())
context['pages'] = BlogPage.objects.live().filter(Q(locale=self.locale), Q(my_tag=default_tag) | Q(my_tag=self.my_tag)).distinct()
return context
```

0 comments on commit e625887

Please sign in to comment.