Forgive me if I'm not describing this correctly - I'm still learning and I've hit a wall with a problem I'm working on. I want to create a way (in the Admin backend for now) for users to show the books they like by fiction/non-fiction, age-range, genre(s) and sub-genre(s). So, a user who liked gothic horror books could select fiction>adult>horror>gothic. Or, a user who likes literally all YA books could select fiction>Young Adult and their selection would cascade down to every genre and sub-genre.
I think the Choices field option and the MultiSelectField functionality might be a start, but there doesn't seem to be a way to add 'layers' to the choices beyond the categories below, and it seems like an inelegant solution when there could be hundreds of choices. I also don't know how it could be presented in the Admin - ideally with checkboxes that can 'open up' to show the sub-categories beneath them.
GENRE_CHOICES = (
('Fiction', (
('FCH', 'Childrens'),
('FMG', 'Middle Grade'),
('FYA', 'Young Adult'),
('FAD', 'Adult'),
)),
('Non-Fiction', (
('NCH', 'Childrens'),
('FMG', 'Middle Grade'),
('FYA', 'Young Adult'),
('FAD', 'Adult'),
)),
)
genres = MultiSelectField(max_length = 100, choices=GENRE_CHOICES, null=True)
I don't even know the name of what I'm looking for here, so if anyone can point me in the right direction that'd be great.
CodePudding user response:
If you want the user to filter the list of books based on their preferences, then see django-filter
https://django-filter.readthedocs.io/en/stable/index.html
(unfortunately I don't have enough reputation to write this to the commertary)
CodePudding user response:
What you are looking for is 'tree structure'. You can define it in multiple ways, for example the way you implement tree in database:
class Tree(models.Model):
parent = models.ForeignKey('self', ...)
But there is better approach, which is somehow similar to what you wanted to use in the first place, please refer to django-mptt TreeNodeMultipleChoiceField