{"id":13520174,"url":"https://github.com/katomaso/django-bit-category","last_synced_at":"2025-04-25T11:03:25.937Z","repository":{"id":151573399,"uuid":"9378380","full_name":"katomaso/django-bit-category","owner":"katomaso","description":"Blazingly fast and simple Category model which uses HierarchicalModel with bitwise IDs","archived":false,"fork":false,"pushed_at":"2017-01-31T08:37:41.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T02:18:56.028Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/katomaso.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2013-04-11T19:22:44.000Z","updated_at":"2017-03-28T13:58:15.000Z","dependencies_parsed_at":"2023-05-29T11:15:31.456Z","dependency_job_id":null,"html_url":"https://github.com/katomaso/django-bit-category","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katomaso%2Fdjango-bit-category","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katomaso%2Fdjango-bit-category/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katomaso%2Fdjango-bit-category/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katomaso%2Fdjango-bit-category/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/katomaso","download_url":"https://codeload.github.com/katomaso/django-bit-category/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250805549,"owners_count":21490183,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:02:13.322Z","updated_at":"2025-04-25T11:03:25.887Z","avatar_url":"https://github.com/katomaso.png","language":"Python","readme":"django-bit-category\n###################\n\n:author: Tomas Peterka\n:licence: GPL\n\nAbstract (and one concrete) ``Model`` with tree-like structure using bitwise ID field.\nThis implementation is **very simple** and **super fast**!\nGiven a category, you can query for related models (products) to the category\nand all it's subcategories by\n`SomeModel.objects.filter(category_id__gte=category.gte, category_id__lt=category.lt)`\n\nGet it\n======\n\nSource: https://github.com/katomaso/django-bit-category/\nPypi: https://pypi.python.org/pypi/django-bit-category/\n\nTechnical details\n=================\n\nThe key idea is to reserve a block of bits in model's ID for different levels of\nhierarchy. In the basic setup we expect 32 bit ID (can be changed via ``ID_BIT_WIDTH``)\nand we give 5 bits for each level (can be changed via ``LEVEL_BIT_WIDTH``).\nThe IDs looks like this::\n\n    XXXXX000000000000000000000000000  # mask for the root category\n    00001000000000000000000000000000  # first root\n    00010000000000000000000000000000  # second root\n    00011000000000000000000000000000  # third root\n\n    00001000010000000000000000000000  # first child of the first root\n    00001000100000000000000000000000  # second child of the first root\n\n    ...and so on\n\nGetting **all** descendants in all levels is in ``hierarchical_instance.descendants``,\nbut under the hood it is as simple as::\n\n    SomeModel.objects.filter(category_id__gte=category.gte, category_id__lt=category.lt)\n\n\nWhat is included in this app\n----------------------------\n\n  * abstract HierarchicalModel which takes care about the magic with IDs\n  * abstract BaseCategory which contains the most usual category implementation\n  * HierarchicalField which you can use for any custom model\n  * HierarchicalWidget which dynamically (via AJAX) creates / deletes select boxes\n  * urls.py and views.py which contains AJAX magic for form field working\n\n\nHow to make it work\n-------------------\n\nIf you just want to use just one of the abstract models then you don't need to do anything special.\nImport the abstract model ``from bitcategory.models import BaseCategory`` and inherit from it in your\nconcrete Model. Then make a foreign key in your another model which is going to use categories::\n\n    # :file: models.py\n    from django.db import models\n    from bitcategory.models import BaseCategory\n\n    class MyCategory(BaseCategory):\n        # BaseCategory already provides fileds name, slug and path\n        class Meta:\n            abstract = False\n\n    class MyProduct(models.Model):\n        # some other fields like price, quantity ....\n        category = models.ForeignKey('myproject.MyCategory', verbose_name=_(\"Category\"))\n\nThat is all to your ``Model`` definition. Now, provided you have a concrete instance of MyCategory in\nvariable ``category``, you can query products within the category and all its subcategories by::\n\n    MyProduct.objects.filter(category_id__gte=category.gte, category_id__lt=category.lt)\n\nor to get products only for the category by::\n\n    MyProduct.objects.filter(category=category)\n\nHowever, if you want to have the awesome dynamic select boxes in your forms with ``category`` in it,\nyou need to do more\n\n  * add the ``bitcategory`` into your INSTALLED_APPS\n  * add ``bitcategory.urls`` into your urls and specify your custom hierarchical\n    model. If you didn't create your custom hierarchical model, then use our pre-built concrete model\n    ``bitcategory.models.Category``. We do that in order to be able to respond to AJAX requests which are sent\n    by the ``bitcategory.fields.HierarchicalField``. The most simple way looks like::\n\n      # :file: urls.py\n      from django.conf.urls import patterns, include, url\n      from myapp.models import YourHierarchicalModel\n\n      urlpatterns = patterns('',\n            url('', include('bitcategory.urls'), {\"model\": YourHierarchicalModel}),\n      )\n\nNow you are ready to show your form with categories in it. First, add\n``bitcategory.fields.HierarchicalField`` in the form. When rendering the form into a page, don't\nforget to include ``{{form.media}}`` into your template for javascripts.\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatomaso%2Fdjango-bit-category","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkatomaso%2Fdjango-bit-category","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatomaso%2Fdjango-bit-category/lists"}