Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

426
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda