Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added django-mptt #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added django-mptt #43

wants to merge 1 commit into from

Conversation

don-mums
Copy link

@don-mums don-mums commented Nov 7, 2012

Optimization of very big sites with houndreds of categories.
Djagno-mptt manages level of category so code that doe's the same in
LFS needs to be removed. Root categories have level == 0

It uses far les queries. When there is only one root category the whole tree can be retrieved with only one query. It is very efficient. Unfortunately it doesn't pass all the tests and I don't have time to play with this code, since all the things I need work as expected. Now you cannot rearrange categories in the admin and I don't know how to fix it.

Optimization of very big sites with houndreds of categories.
Djagno-mptt manages level of category so code that doe's the same in
LFS needs to be removed. Root categories have level == 0
@diefenbach
Copy link
Owner

Thanks!

Looks good so far but we need migrations from 0.7 to dev.

Kai

@don-mums
Copy link
Author

Sure thing, that time I was in a hurry so it wasn't perfect. I hope I'll commit better version this or next week.

@diefenbach
Copy link
Owner

Cool!

Am 27.01.2013 um 13:05 schrieb don-mums [email protected]:

Sure thing, that time I was in a hurry so it wasn't perfect. I hope I'll commit better version this or next week.


Reply to this email directly or view it on GitHub.

@baffolobill
Copy link
Contributor

Migration to MPTT looks very simple:

def migrate_category_mptt(self, application, version):
    """
        Source: https://github.com/django-mptt/django-mptt/issues/195
    """
    from lfs.catalog.models import Category

    # If there is no 'rebuild' method,
    # an old version of MPTT is installed
    # or MPTT is not connected to the Category model
    if not hasattr(Category.objects, 'rebuild') \
        or not (hasattr(Category, 'tree') and hasattr(Category.tree, 'rebuild')):
        return False

    # There is no need here to specify `db_index` and `editable` params.
    # I did this to keep an original definition as notes for an extra actions.
    # For example, do not forget to create index for the fields.

    # Add new fields
    db.add_column("catalog_category", "lft", models.PositiveIntegerField(db_index=True, editable=False))
    db.add_column("catalog_category", "rght", models.PositiveIntegerField(db_index=True, editable=False))
    db.add_column("catalog_category", "tree_id", models.PositiveIntegerField(db_index=True, editable=False))

    # Category already has 'level' field, but type is 'PositiveSmallInteger', so change it to a correct one
    db.alter_column("catalog_category", "level", models.PositiveIntegerField(db_index=True, editable=False))

    db.create_index("catalog_category", ["lft"])
    db.create_index("catalog_category", ["rght"])
    db.create_index("catalog_category", ["tree_id"])
    db.create_index("catalog_category", ["level"])

    # Set default values for the added fields
    Category.objects.all().update(lft=0, rght=0, tree_id=0, level=0)

    # Fixes values for 'lft', 'rght', 'tree_id' and 'level' fields
    Category.objects.rebuild()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants