21👍
✅
not used django mptt in a while, but given that a leaf node may be identified by right == left + 1
, you should be able to filter for this using an F()
expression
- Django annotate specific keys in json fields
- Relation does not exist error in Django
- Django + Forms: Dynamic choices for ChoiceField
- Database table names with Django
- Soft deleting objects in django
2👍
Unoptimal solution:
Category.objects.filter(id__in=[category.id for category in Category.objects.all() if category.is_leaf_node()])
- Django Newsletter App
- How to test django model method __str__()
- Multiple User Types For Auth in Django
- OSError: dlopen(libSystem.dylib, 6): image not found
0👍
My little snippet for django mptt
from django.db import models
class CategoryManager(models.Manager):
def get_leaf(self, level=2):
return self.filter(level__lte=level).order_by('tree_id','lft').all()
class Category(models.Model):
objects = CategoryManager()
profit, use it: Catalog.objects.get_leaf()
- Align radio buttons horizontally in django forms
- Django can't access raw_post_data
- How do I tell Django to save my test database?
0👍
From docs:
is_leaf_node()
Returns True
if this model instance is a leaf node (it has no children), False
otherwise.
- AWS Elastic Beanstalk Container Commands Failing
- Django, division between two annotate result won't calculate correctly
Source:stackexchange.com