Mr Dot
Mr Dot

Reputation: 277

Django poll models choice, how change my models?

I am new in django and i need some helps. I just can't figure out the situation, since I have little experience in django, perhaps this question is too simple but not for me. I wrote a survey with the ability to choose one option, but I do not know how to add the option of a text response, an answer not only with a choice of one option, an answer with a choice of several options

models.py

class Poll(models.Model):
    title    = models.CharField(max_length=200)
    question = models.TextField()
    option_one = models.CharField(max_length=50)
    option_two = models.CharField(max_length=50)
    option_three = models.CharField(max_length=50)
    option_input = models.CharField(max_length=50, null=True, blank=True)
    option_one_count = models.IntegerField(default=0)
    option_two_count = models.IntegerField(default=0)
    option_three_count = models.IntegerField(default=0)
    active_from = models.DateTimeField(auto_now_add=True, null=True)
    active_for = models.IntegerField(default=0)

views.py

def create(request):
    if request.method == 'POST':
        form = CreatePollForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect ('home')

    else:   
        form = CreatePollForm()
    context = {'form':form}
    return render(request, 'poll/create.html', context)

def update_vote(request,  poll_id):
    poll = Poll.objects.get(pk=poll_id)
    form = CreatePollForm(instance=poll)
    if request.method == 'POST':
        form = CreatePollForm(request.POST, instance=poll)
        if form.is_valid():
            form.save()
            return redirect('home')
    context = {'form': form}
    return render(request, 'poll/create.html', context)

def delete_vote(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    if request.method == 'POST':
        poll.delete()
        return redirect('home')
    context = {'poll': poll}
    return render(request, 'poll/delete_votting.html', context)



def vote(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    if request.method == 'POST':
        if poll.is_active:
            selected_option = request.POST['poll']
            if selected_option == 'option1':
                poll.option_one_count += 1
            elif selected_option == 'option2':
                poll.option_two_count += 1
            elif selected_option == 'option3':
                poll.option_three_count += 1
            else:
                return HttpResponse(400, 'Invalid form')
            poll.save()
            return redirect('results', poll.id)   
        else:
            messages.info(request, 'The poll is closed')         
    context = {'poll':poll}
    return render(request, 'poll/vote.html', context)


def results(request,poll_id):
    poll = Poll.objects.get(pk=poll_id)
    context = {'poll':poll}
    return render(request, 'poll/results.html', context)

Upvotes: 0

Views: 255

Answers (2)

AMG
AMG

Reputation: 1646

I think there a couple of ways to do it. One would be to use inheritance, having a base poll, and other models that inherit from it for the different types of questions (multiple choice, long text answers, one from a list etc).

Another option would be to have a 'type' field in poll which was a ForeignKey to something like 'PollType'. You'd base your subsequent logic on which poll type was selected, somehow storing the results in either one giant table, or multiple tables (one for each question type).

In any case, when you see yourself adding attributes like option1_question, option2_question, option3_question or option1_response, option2_response, option3_response, assume that there is probably a better way having having something like a questions model and a response model with an index or line number. This allows you to not be limited to the maximum number of attributes you decided to 'hardwire' into your attributes. As with all things, there are exceptions and in some cases the 'hardwiring' of the options may be the method to use for a particular use case.

I would start by drawing out what you are going to need to store the responses for each of the different types of questions then start to model out the solution that fits best for your application. Some design decisions are affected by how you want to report the data, is there a need to tally and someone wins (like a pick your top 3 candidates in order type question), or if you want to live display a chart with the most common selections (like in a presentation asking the participants to pick all of their ice cream flavours).

It is not a trivial undertaking and can get as complicated as you like (multiple choice with pick between 2 and 5 of the following choices), so take your time with it, one question type at a time. When you get stuck on one, ask again about that specific question type.

Upvotes: 1

Tiana987642
Tiana987642

Reputation: 754

Please read about TextChoices, I think it's more suitable

https://docs.djangoproject.com/en/3.1/ref/models/fields/

Upvotes: 0

Related Questions