I am trying to make a todo app in django but I am not able to get a dropdown to set a category populate.
This is what the form looks like when I render everything:
What I am expecting was that the dropdown would have the options 'Personal' and 'Professional'
This is my model:
from django.db import models
class Category(models.Model):
CATEGORY = (('Personal', 'Personal'), ('Professional', 'Professional'))
type = models.CharField(max_length=100, null=True, choices=CATEGORY)
def __str__(self):
return self.type
class Item(models.Model):
created_at = models.DateTimeField(auto_now=True)
task = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
category = models.ForeignKey(
Category, on_delete=models.SET_NULL, null=True)
def __str__(self):
return (f"{self.created_at}: {self.task}")
This is the form I am trying to use:
from .models import Item
from django import forms
class AddItem(forms.ModelForm):
class Meta:
model = Item
fields = ['task', 'category']
This is the template:
<div>
<h1>Your todo task</h1>
<br>
<form method="POST" action="{% url 'home' %}">
{% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-secondary">Add a task</button>
</form>
</div>
Thank you for your time and any information you can provide.
This video (timestamped) answered my question.
In the end I think using a tuple for the list was not the right call, specifically this part:
CATEGORY = (('Personal', 'Personal'), ('Professional', 'Professional'))
type = models.CharField(max_length=100, null=True, choices=CATEGORY)
It was better to have it be a char field and then just add the categories through the admin panel.