Django: using mptt-django to allocate products to categories in a many-to-many relationship -
i have many-to-many relationship between products , categories. using mptt manage display of categories on admin (which works fine) , can allocate products categories.
while categories working fine, it's product side i'm having problems with.
in models.py have (truncated brevity):
from mptt.models import mpttmodel,treeforeignkey,treemanytomanyfield class productcategory(mpttmodel): parent = treeforeignkey('self', null=true, blank=true) class product(models.model) categories = treemanytomanyfield(productcategory) in admin.py have
from django.forms import checkboxselectmultiple .models import product,productcategory django.db import models django.contrib import admin mptt.admin import mpttmodeladmin mptt.models import treemanytomanyfield class productadmin(admin.modeladmin): formfield_overrides = { treemanytomanyfield:{'widget':checkboxselectmultiple}, } this gets job done of categories displayed checkboxes not want since don't want add products categories have sub categories.
what want display of categories such can add products categories don't have children, in example
category 1 category 2 sub sub category 3 sub and labels
category 1 category 2 > catgory 2 sub > category 2 sub sub category 3 > category 3 sub many help!
update
hi, realise me question may broad!
as django newbie, think need create custom widget possibly overriding checkboxselectmultiple widget, can apply 'categories' model attribute.
at moment, using formfield_overrides treemanytomanyfield discussed above
formfield_overrides = { treemanytomanyfield:{'widget':checkboxselectmultiple}, } not not give desired result rather using standard checkboxselectmultiple treemanytomany fields, can specify widget 'categories' attribute?
notwithstanding aside, possible show me how can create custom widget, passing information category tree , selected states (for product has been allocated categories)? i've been trying sort myself have been struggling - appreciated!
update following karantsthr comment
here screenshot of categories - in showing categories no children, can show parents these categories in label described above?
update 2 karantsthr
here screenshot implementation.
modeladmin flexible, can specify custom modelform form option in productadmin (form modeladmin option ) filter out categories not having child category.
so, think don't need custom widget solve problem, change admin.py
from django import forms django.forms import checkboxselectmultiple .models import product,productcategory django.contrib import admin mptt.admin import mpttmodeladmin mptt.models import treemanytomanyfield class filtercategories(forms.modelform): class meta: model = product fields = '__all__' def __init__(self,*args,**kwargs): super(filtercategories, self).__init__(*args,**kwargs) self.fields['categories'].queryset = productcategory.objects.filter(children=none) class productadmin(admin.modeladmin): form = filtercategories formfield_overrides = { treemanytomanyfield:{'widget':checkboxselectmultiple},} admin.site.register(product,productadmin) admin.site.register(productcategory,mpttmodeladmin)
update 1
need add related_name in parent field of productcategory class in models.py
productcategory class in models.py below
class productcategory(mpttmodel): name = models.charfield(max_length=100) parent = treeforeignkey('self', null=true, blank=true, related_name='children') def __str__(self): return self.name update 2
can change str method productcategory follow labels
def __str__(self): try: ancestors = self.get_ancestors(include_self=true) ancestors = [i.name in ancestors] except: ancestors = [self.name] return ' > '.join(ancestors[:len(ancestors) + 1]) note not radical approach gaining label ( object's name ) of choice, update answer if find better solution.



Comments
Post a Comment