Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

419
Views
Adding field to ManyToMany relationship Django ORM

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.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!