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

223
Visualizações
Is there a way to have dictinary-like field in Django model?

Suppose, I am making a movie rating app. A logged-in user should be able to rate the movie with stars (in the range 1 to 5).

I want to quickly access all the rater's name along with their rating. If a user rates the movie again, the rating should be updated. At the same time, if a user decides to withdraw his rating i.e. provide zero rating, I would like to remove the entry from the field.

I believe dictionary would be the best choice to achieve the same. However, I am open for suggestions.

I also want a user to see all the movies the he/she has rated along with the rating.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You should create an ForeignKey field between the user model and the voting model. To only allow a single vote, per user; per movie, you might want to create a unique key constraint, on the voting model, over the userid and the movieid.

The vote should then contain a relation to the movie and a rating. If a user withdraws his vote you remove it from database.

Using a dictionary will lead to problems for you as you will have multiple movies.

To increase the speed and performance counting the votes you might want to take a look at caching and simply cache the number of votes and update the number every time a vote was added/withdrawn by a user on the specific movie.

over 4 years ago · Santiago Trujillo Relatório

0

You can use: update_or_create() method with django ORM. https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create

over 4 years ago · Santiago Trujillo Relatório

0

If the database you are using is Postgres, then you can make use of JSONField

over 4 years ago · Santiago Trujillo Relatório
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