diefenbach / django-lfs

An online-shop based on Django
http://www.getlfs.com
BSD 3-Clause "New" or "Revised" License
622 stars 222 forks source link

Added django-mptt #43

Open don-mums opened 12 years ago

don-mums commented 12 years ago

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.

diefenbach commented 11 years ago

Thanks!

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

Kai

don-mums commented 11 years ago

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 commented 11 years ago

Cool!

Am 27.01.2013 um 13:05 schrieb don-mums notifications@github.com:

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 commented 10 years ago

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()