ManytoMany Relation and Custom widget in Django

711 views
Skip to first unread message

sevenr...@gmail.com

unread,
Nov 17, 2017, 7:34:48 AM11/17/17
to Django developers (Contributions to Django itself)
Hello,

First, I apologize for posting on Django Developers but I asked on Django Users, Django IRC, stackoverflow (in multiple ways/times) and I'm blocked for more than 2 weeks.

I think the question, is too advanced for the other  forums/groups and hope someone here can help me.


I have two models Category and Products.

  • a Product can have multiple Categories
  • a Category can have multiple Products.
  • Categories have a circular Foreign key, to itself.
  • not all Categories have the same depth level

Example:

    Category A
        - Category A_1
        - category A_2
            - Category A_2_1
    Category B
    Category C
        - Category C_1


        class Product:
            categories = models.ManyToManyField(
Category)
            name = models.CharField(max_length=255)
       
        class Category:
            categories = models.ForeignKey(self)
            name = models.CharField(max_length=255)
   
    As Form I use a ModelForm:
   
        class ProductForm(ModelForm):
   
                class Meta:
                    model = Product
                    fields = ['categories', 'name', 'short_description', 'description']
                    widgets = {
                        'categories': MyWidget,
                    }

What I want to achieve:

I want to implement a conditional select (narrow options):

  1. Only top parent Categories(level 0 A,B,C) are available
  2. The user select a parent Category.  If the parent has children a new Select box appear with his children(category level 1 A1,C1)
  3. The user select a level 1 Category (A1,C1). If the parent has children a new Select box appear with his children(level 2 A2)

  • The process is repeated until no children are availavable(recursive), an user select the "smallest" category in the tree
  •  A new button is available for the user to add more categories and start the 1-3 process again
  • I want to do the select,add new select using JavaScript
  • On Form Submit I want to send only the last children categories


Options I thought:

  1. Change the ManyToMany coresponding default Fields - looks like there are no good hooks and/or inheritance
  2. Use a non-default custom Field instean of ManytoMany(like Charfield) - more complex on clean,saving Form
  3. Change/Inherit the SelectMultiple Widget. My issues is how to send the data to the default Field on submit, and get/show it on edit
 
I need some help as code and indication how to do this.

Tim Graham

unread,
Nov 17, 2017, 8:05:32 AM11/17/17
to Django developers (Contributions to Django itself)
Please don't abuse this mailing list as an "advance support channel".

sevenr...@gmail.com

unread,
Nov 17, 2017, 9:39:27 AM11/17/17
to Django developers (Contributions to Django itself)


I’m sorry, desperate move, because I couldn’t find any proper channel for this.


Can you guide me to a proper channel that can answer my question, besides the ones above.


Thank you.

Shai Berger

unread,
Nov 17, 2017, 11:28:32 AM11/17/17
to django-d...@googlegroups.com
On Friday 17 November 2017 15:53:10 sevenr...@gmail.com wrote:
> I’m sorry, desperate move, because I couldn’t find any proper channel for
> this.
>
>
> Can you guide me to a proper channel that can answer my question, besides
> the ones above.
>

If you cannot find an answer on the public channels and forums, it indicates
that your problem is, on one hand, not very easy to answer, and on the other
hand, not interesting enough to the people who could answer it. I hope you
remember everyone here is a volunteer.

Perhaps you should look for a consultant who isn't a volunteer.

HTH,
Shai.
Reply all
Reply to author
Forward
0 new messages