I may need help developing my models and their relationships.
Short review
- Book : The names of the books (for example, Lord of the Rings).
- Tag : tags associated with books (e.g. Fantasy, Wizard, Epic Battle)
- Aspect : aspects related to tags, or, in other words, things that users can evaluate when a specific tag occurs, for example:
- for "Fantasy" tags can be "World Detail" and "Time"
- for the "Epic fight" tag, aspects can be "Gore Level" and "Fight tension".
Each tag can be used in several copies of the Book (for example, “Lord of the Rings” and “Discwich”, both have a tag “Fantasy”).
Each aspect can be used in several instances of Tag (for example, "Fantasy" and "Scifi", both have the "World Detail" aspect).
Here is a descriptive image:
(wow that's big)
"Why do you need an extra BookAspect table?" you can ask?
Because I want to store user ratings for every aspect related to a particular book.
This is the main problem here. I want to simulate this in Django, and this is what I got so far:
class Book(models.Model): title = models.CharField(max_length=100, unique=True) tags = ManyToManyField(Tag)
In addition to the models, I created a m2m_changed
signal m2m_changed
for action="post_add"
:
@receiver(m2m_changed, sender=Book.tags.through) def m2m_changed_book(sender, instance, action, reverse, pk_set, **kwargs): if action is not 'post_add' or reverse: return
Although this should work, I will need additional logic to eliminate the deleted tags.
But the real annoyance is that I have to manually add relationships between books and aspects so that I can store user ratings. Naturally, I need different grades for the same aspect of different books.
Questions
1. Is this the right way to do this, am I missing something or is it not as difficult as I think?
2. Is it possible to “automate” the Book-Aspect relationship, so I don’t need to manually update the relationship?
3. How can I simulate it in Django?