csev / dj4e-samples

Django For Everybody Sample Code
https://samples.dj4e.com/
MIT License
559 stars 593 forks source link

forum app | Doubled tables with the same purpose #20

Open LiorA1 opened 3 years ago

LiorA1 commented 3 years ago

In the forums app, if we take a look at the models.py module, one can see there two models: Forum and Comment. In the Forum model defined 'comments' ManyToMany field. And in the Comment a 'forum' ForeignKey field.

According to Django Docs, a ForeignKey to a model (in our case: Forum) auto-generates the reverse-access-manager (with the name: _set ). Docs: https://docs.djangoproject.com/en/3.2/topics/db/queries/#following-relationships-backward (It exists also in the m2m field)

Its actually a table that Django generates for us. In our case: 'comment_set'. We can see that exist two tables with the same purpose ('comment_set' and 'comment'), so we basically create a situation with redundant table.

Image here: https://github.com/LiorA1/Django/blob/main/dj4e-samples/forums/DJ4ESpecialization.png Fix Suggestion here: https://github.com/LiorA1/Django/blob/main/dj4e-samples/forums/models.py

Thanks

P.S: The same issue exists in the favs App. P.S 2: For Non C.S Grads, it maybe be beneficial to see it explicitly.

csev commented 3 years ago

This is a great comment. I agree with your "P.S." statements. Let me figure out where to use this approach - I think that since it is the "Django way" - it would be good to slowly switch to this pattern.