I'm planning to use Django MPTT with a multi-tenant architecture, so there will be many site home pages, and I'd like not to have to rebuild the entire page
table's mptt feilds every time a user changes the location of their About Us
page. I'd rather rebuild just that user's website. Is this possible?

- 55,146
- 59
- 179
- 257
2 Answers
Just use a different tree_id
value for each tree.

- 588,541
- 66
- 880
- 895
-
The documentation states ... "you should consider the tree id to be volatile" I guess it will work but it seems dangerous to me http://django-mptt.github.io/django-mptt/technical_details.html#tree-id – rayed Feb 01 '14 at 15:03
I tried it, I loaded a 2nd tree into the same table. On one hand, Django MPTT gave new tree_id's to the new tree branches. So in that way, it seemed to work. On the other hand, now the names of the nodes do NOT show up using the template from the Django MPTT site. (I attach a screen shot.) The template worked perfectly when there was only one tree in the table. I made no change to any code and such, I just put a 2nd tree in the table, and presto! no names on the listing.
The structure of the tree looks the same. I also get no names when I access the other tree, which I can access OK, except there are no names.
Here is the model definition:
class EbayCategory(MPTTModel):
iCategoryID = models.PositiveIntegerField( 'ebay category number',
db_index=True )
name = models.CharField(
'ebay category description', max_length = 50 )
iLevel = models.PositiveSmallIntegerField(
'ebay level (top is 1, lower levels are bigger numbers)' )
iParentID = models.PositiveIntegerField( 'ebay parent category' )
bLeafCategory = models.BooleanField( 'leaf category?' )
iTreeVersion = models.PositiveSmallIntegerField(
'category tree version' )
iMarket = models.ForeignKey( Market, verbose_name = 'ebay market' )
iSupercededBy = models.PositiveIntegerField(
'superceded by this ebay category', null = True )
parent = TreeForeignKey( 'self',
null=True, blank=True, related_name='children',
db_index=True)
def __str__(self):
return self.name
class Meta():
verbose_name_plural = 'ebay categories'
db_table = 'ebay_categories'
unique_together = ('iCategoryID', 'iMarket',)
class MPTTMeta:
order_insertion_by = ['name']
All the columns except parent come in from the ebay API.
At this point, I have no categories that have been superceded, so the iSupercededBy column is null for all rows. I have not experienced an ebay catetory list update yet, so all the rows for each market are the same ebay tree version (the current one).
The view selects by market and ebay tree version (both values come from the Markets table):
def show_ebay_categories(request, sMarket):
iMarket = Market.objects.get( cMarket = sMarket.upper() )
context = { 'nodes':EbayCategory.objects.filter(
iMarket = iMarket, iTreeVersion = iMarket.iCategoryVer ) }
return render(request, 'ebaycategories/ebay_categories.html', context)
Running the same code, against a copy of the database from before adding in the 2nd tree, the names show up.
It seem adding the 2nd tree corrupted the table in a way that rebuild cannot fix. I deleted the 2nd tree. Still no names. I rebuilt the tree:
How do I rebuild my django-mptt tree?
Still no names. I truncated the table, then re-uploaded the first tree (ebay USA categories). Still no names. I rebuilt the tree again. Still no names.
What worked: Adding a root node to each category listing (tree) when importing the data. (The data downloaded from ebay did not include a root node,)
Note "EBAY-US version 117 Root" at the top of the listing.
I generated the listing AFTER importing into the same table a 2nd tree. So yes it can work.

- 517
- 5
- 11