0

I am trying to use TreeBeard's built in Form's with django forms (not admin). I specifically wanted to replace the rendering of a Select ForeignKey field with TreeBeard forms format. I thought I could do this by declaring the field in my ModelForm, but I've had no success. I'm new to django so my understanding is limited.

These are my initial classes in my forms.py

MyCategories = movenodeform_factory(Category)

class CreatePost(ModelForm):

    class Meta:
        model = Post
        fields = ['title', 'category', 'region', 'content', ]

I tried implementing it by declaring the category field in the beginning but this clearly isn't the way to do it. The declaration does return an html formatted category list, but I can't replace the Post category (which is a ForeignKey)with it.

class CreatePost(ModelForm):
category = movenodeform_factory(Category)

class Meta:
    model = Post
    fields = ['title', 'category', 'region', 'content', ]

The reason I want to use TreeBeard forms is because of the way it nests the fields according to the category hierarchy.

SOLVED: This ended up being much simpler than I realized.

class CreatePost(ModelForm):
CHOICES = MoveNodeForm.mk_dropdown_tree(Category)
category = ChoiceField(choices=CHOICES)

class Meta:
    model = Post
    fields = ['title', 'category', 'region', 'content', ]
pgwalsh
  • 31
  • 3

1 Answers1

2

The solution was right in front of me. I just needed to create a list using mk_dropdown_tree and use it in a ChoiceField. I hope this might help someone someday.

class CreatePost(ModelForm):
    CHOICES = MoveNodeForm.mk_dropdown_tree(Category)
    category = ChoiceField(choices=CHOICES)

    class Meta:
        model = Post
        fields = ['title', 'category', 'region', 'content', ]
pgwalsh
  • 31
  • 3
  • I found a better way of solving this issue. This is the correct answer for what I was trying to accomplish. https://stackoverflow.com/a/48108929/6156259 – pgwalsh Sep 03 '18 at 00:14