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

111
Vistas
Store two types of value in single Django model field

I am learning django by creating a polling application. For starters I have three models User, Question and Choices.

class User(....):
    ....
    Questions_voted = models.ManyToManyField(Question)
    ....

this means that a user to vote for as many questions as he wants. Next is The Question Model which is simple and just store question text and number of people voted for it.

class Question(models.Model):
    question_text = models.CharField(max_length=500)
    vote_count = models.IntegerField(...)

next is Choices which have a choice_text and refers to the question it belongs to.

class Choice(models.Models):
    choice_text = models.CharField(models.Model)
    question = models.ForeignKey(Question,...)

Now I want to store information about what choice a user selected for a question. It is very clear when you think of it but I am stuck because of lack of knowledge of types of fields in django models.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I recommend you to change model schema. Here is suggested model:

Suggested ER model

I added UserVote model that holds information about Question and Choice selected by User.

from django.conf import settings

class UserVote(models.Model):
    question = models.ForeignKey('Question')
    choice = models.ForeignKey('Choice')
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

EDIT: How to read suggested schema: User can have many UserVotes (there is Many-To-One relation – in Django you simply add ForeignKey to UserVote). Each UserVote must have one-and-only one Question and Choice. The rest is your old schema. Btw I used Crow's foot notation (https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#Crow.27s_foot_notation).

over 4 years ago · Santiago Trujillo Denunciar
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