mptt

django-mptt raises django.db.utils.IntegrityError: null value in column “lft” violates not-null constraint

蹲街弑〆低调 提交于 2020-02-04 01:19:08
问题 Conditions: Django==1.8.7 и django-mptt==0.8.0. There is a model: class Tree(mptt_models.MPTTModel): name = models.CharField(max_length=120, unique=True) slug = models.SlugField(max_length=256, unique=True) parent = mptt_models.TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True) class MPTTMeta: order_insertion_by = ['name'] I can fill it with admin interface and show on a site pages. I can fill it with django shell: Python 2.7.3 (default, Jun 22 2015, 19:43

Django MPTT - how to query based on URL segments

狂风中的少年 提交于 2020-01-05 11:49:28
问题 I have: Home Things with Fur Dog Horse Basket Ball Games Twenty One Horse Assuming the slug for each is title.lower().replace(' ', '-') , my URLs would be: /home/ /home/things-with-fur/ /home/things-with-fur/dog/ /home/things-with-fur/horse/ /home/basket-ball-games/ /home/basket-ball-games/twenty-one/ /home/basket-ball-games/horse/ The slug field is unique_together with the parent . I need to, based on the URL segements, query for the correct category. I can't simply query for the basket ball

Deeply nested subqueries for traversing trees in MySQL

£可爱£侵袭症+ 提交于 2020-01-02 05:25:09
问题 I have a table in my database where I store a tree structure using the hybrid Nested Set (MPTT) model (the one which has lft and rght values) and the Adjacency List model (storing parent_id on each node). my_table (id, parent_id, lft, rght, alias) This question doesn't relate to any of the MPTT aspects of the tree but I thought I'd leave it in in case anyone had a good idea about how to leverage that. I want to convert a path of aliases to a specific node. For example: "users.admins.nickf"

Django treebeard what are differences between AL, NS, MP

旧街凉风 提交于 2019-12-31 08:08:05
问题 I'm trying to make a model for categorizing some objects. I already tried using django-mptt to easily retrieve related categories, and now I'm searching different solutions to find the best one. I can't find out though what are main differences between Materialized Path, Adjacency List and Nested Set. Wikipedia didn't give me a short answer, all I know is mptt is probably Nested Set... Can anyone explain it to me in few words ? 回答1: It's easier to explain with examples than with a few words.

efficient function to retrieve a queryset of ancestors of an mptt queryset

a 夏天 提交于 2019-12-21 03:32:36
问题 Does anybody have an efficient algorithm to retrieve all ancestors of an mptt queryset? The best I could think of so far is something like this: def qs_ancestors(queryset): if isinstance(queryset, EmptyQuerySet): return queryset queryset_aggs = queryset.values_list('tree_id', 'level').annotate(max_lft=Max('lft'), min_rght=Min('rght')) new_queryset = queryset.none() for tree_id, level, max_lft, min_rght in queryset_aggs: ancestors = MyModel.objects.filter( tree_id=tree_id, level__lt=level, lft

MySql - Self Join - Full Table Scan (Cannot Scan Index)

烂漫一生 提交于 2019-12-13 08:01:54
问题 I have the following self-join query: SELECT A.id FROM mytbl AS A LEFT JOIN mytbl AS B ON (A.lft BETWEEN B.lft AND B.rgt) The query is quite slow, and after looking at the execution plan the cause appears to be a full table scan in the JOIN. The table has only 500 rows, and suspecting this to be the issue I increased it to 100,000 rows in order to see if it made a difference to the optimizer's selection. It did not, with 100k rows it was still doing a full table scan. My next step was to try

PHP converting multidimensional array to Modified Pre-order Traversal Tree

二次信任 提交于 2019-12-12 01:49:52
问题 I have a multidimendional array that I want to transform to a Modified Pre-order Traversal Tree: $data = array( "MTGO" => array("Magic Online" => array("MTGO Masters Edition", "MTGO Masters Edition II", "MTGO Masters Edition III", "MTGO Masters Edition IV")), "Expansions" => array("Ice Age Cycle" => array("Coldsnap Theme Decks", "Ice Age", "Alliances"), "Theros" => array("Theros")), "unclassified" => array("Portal", "Eight Edition") ); I've tried by looping through the nested arrays, or using

How to use MPTT for django to get all children?

白昼怎懂夜的黑 提交于 2019-12-11 12:11:42
问题 I'm trying to get all the children of a category: def list_sub(self, category_name): # this will return the parent if exists category = Category.objects.filter(seo_title__exact = seo_title).filter(lang__exact = 'pt-PT').filter(level__exact = 1) if category: # but this doesn't work and in the documentation there are no examples # of how to get it. See link about the method sub_categories = category.get_children() http://django-mptt.github.com/django-mptt/models.html#get-children Update1: qc =

Problem using django mptt

眉间皱痕 提交于 2019-12-11 05:32:16
问题 I am having problem implementing django mptt. Here is my model: class Company(models.Model): name = models.CharField( max_length=100) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') mptt.register(Company, order_insertion_by=['name']) And class Financials(models.Model): company = models.ForeignKey(Company, related_name="financials") year = models.IntegerField() revenue = models.DecimalField(max_digits = 10, decimal_places = 2) So what I am looking at is how

MPTT ( Modified Preorder Tree Traversal) issue in PHP

一世执手 提交于 2019-12-11 00:26:30
问题 My first post here! Seems like this is the place to get wise ;) I am currently in the middle of some testing with my first ever attempt to try the MPTT (Modified Preorder Tree Traversal) approach to storing data in my Mysql database with the help of PHP. However, I am trying to find out the most performance-oriented way to get all list elements on a specific level, with a specific parent. That could translate into getting the categories Saab and Chrysler from the image below, if the parent