Created
January 31, 2018 21:01
-
-
Save cnk/5625a761993476a691bcb708ef468f6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@register_snippet | |
class Author(models.Model): | |
first_name = models.CharField(max_length=128) | |
last_name = models.CharField(max_length=128) | |
site = models.ForeignKey('wagtailcore.Site', on_delete=models.CASCADE) | |
panels = [ | |
MultiFieldPanel([ | |
FieldPanel('first_name', classname='col6 first-name'), | |
FieldPanel('last_name', classname='col6 last-name'), | |
], classname='author-name'), | |
] | |
class Meta: | |
unique_together = ["first_name", "last_name"] | |
ordering = ['last_name', 'first_name'] | |
def __str__(self): | |
return "{1}, {0}".format(self.first_name, self.last_name) | |
@classmethod | |
def listing_queryset(cls, request): | |
return cls.objects.filter(site=request.site) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Then make the following change (original just calls items = model.objects.all() for all models) | |
def choose(request, app_label, model_name): | |
model = get_snippet_model_from_url_params(app_label, model_name) | |
if getattr(model, 'listing_queryset', None) and callable(model.listing_queryset): | |
items = model.listing_queryset(request) | |
else: | |
items = model.objects.all() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment