Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

421
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda