0

So I'm building out a database where I need to have a field related to a specific ManyToMany relationship, but I don't know how to translate this into the Django ORM. For example, I have multiple Articles that can go with multiple Publications, but I want to see which article got the most views.

I figure that I could build the database like so:

+------------+--------+--------+-------+
| art_pub_id | art_id | pub_id | views |
+------------+--------+--------+-------+
| 101        | 201    | 301    | 6     |
+------------+--------+--------+-------+
| 102        | 201    | 302    | 4     |
+------------+--------+--------+-------+
| 103        | 202    | 301    | 8     |
+------------+--------+--------+-------+

Or I could do this

+------------+--------+--------+
| art_pub_id | art_id | pub_id |
+------------+--------+--------+
| 101        | 201    | 301    |
+------------+--------+--------+
| 102        | 201    | 302    |
+------------+--------+--------+
| 103        | 202    | 301    |
+------------+--------+--------+

+----+------------+-------+
| pk | art_pub_id | views |
+----+------------+-------+
| 1  | 101        | 6     |
+----+------------+-------+
| 2  | 102        | 4     |
+----+------------+-------+
| 3  | 103        | 8     |
+----+------------+-------+

I'm struggling with how to translate this to the Django ORM. I have the Article with the ManyToManyField pointing to the Publication, but I don't know where or how to place the views IntegerField. I have tried having a Publication_Article class that has two ManyToMany fields and my views field, but I'm not sure that is the right way to be doing it.

I also fully recognize that I may be approaching the problem wrong with my database design, in which case knowing the best way to do this would be invaluable for me.

Logan Waite
  • 423
  • 5
  • 12
  • 2
    Are you familiar with the manyToMany Through capability? If the Views are basically number of views for article X when shown in publication Y, that would seem to make the most sense. – Foon Apr 26 '17 at 21:12
  • @Foon I was not, but after reading the documentation for it (guess I need to be more thorough when going through the first time) it does look like that's what I could be looking for. – Logan Waite Apr 26 '17 at 21:33
  • @LoganWaite maybe you could be interested to read about this matter in this question http://stackoverflow.com/questions/3368442/how-do-i-access-the-properties-of-a-many-to-many-through-table-from-a-django-t – Angel F Apr 26 '17 at 21:49

0 Answers0