I am trying to add more information to a form dropdown built using Django forms. A basic explanation of the program is:
- The user logs in
- The user can create ‘decks’ that are to be displayed on the ‘game_tracking’ page
- The user goes to the ‘game_tracking’ page, and the first dropdown loads all of the names of decks that the user created as well as the additional info from the model ‘Deck’ of if it is/isn’t a cEDH deck.
I am unsure of how to expose whether the deck’s information of is_cedh, in addition to the name, in the dropdown
This is what it currently shows when I expand the dropdown
This, ideally, is what I am attempting to do.
Thanks in advance for taking the time to read/answer. I have been working on this for longer than I would like to admit…
This is my models.py file.
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
profile_pic = models.ImageField(null=True,
blank=True,
default='Default.png',
upload_to='images/')
user = models.ForeignKey(User,
max_length=10,
on_delete=models.CASCADE,
null=True)
def __str__(self):
return str(self.user)
class Deck(models.Model):
deck = models.CharField(max_length=150)
is_cedh = models.BooleanField(default=False)
user = models.ForeignKey(User,
max_length=10,
on_delete=models.CASCADE,
null=True)
def __str__(self):
return str(self.deck)
class Game(models.Model):
NUM_PLAYERS_CHOICE = [
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6)
]
OUTCOME_CHOICE = [
('Win', 'Win'),
('Loss', 'Loss'),
('Draw', 'Draw'),
('Incomplete', 'Incomplete'),
]
# ForeignKeys
deck = models.ForeignKey(Deck, on_delete=models.CASCADE, null=True)
wincon = models.ForeignKey(Wincon, on_delete=models.CASCADE, null=True, blank=True)
num_players = models.IntegerField(choices=NUM_PLAYERS_CHOICE, default=4)
outcome = models.CharField(max_length=10, choices=OUTCOME_CHOICE, default='Win')
Here is my forms.py file
class GameForm(forms.ModelForm):
def __init__(self, user, *args, **kwargs):
super(GameForm, self).__init__(*args, **kwargs)
self.fields['deck'].queryset = Deck.objects.filter(user=user)
class Meta:
model = Game
exclude = {'date', 'time', }
labels = {
'deck': 'What deck',
'num_players': '# of Players',
'outcome': 'Outcome',
},
This is my views.py
@login_required(login_url='my-login')
def game_tracker(request):
user_decks = Deck.objects.filter(user=request.user)
if request.method == "POST":
form = GameForm(request.user, request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = GameForm(request.user)
context = {'form': form, }
return render(request, 'gametracker_app/gametracker.html', context=context)
And this is my HTML.
{% block content %}
<form action=""
method="POST">
{% csrf_token %}
<div class="container-sm p-4">
<div class="card"
style="">
<div class="card-body">
{{ form.deck|as_crispy_field }}
<br>
<a href="{% url 'my-decks' %}"
class="">
<p class="">Add new deck?</p>
</a>
<div class="flex-container">
<div class="row">
<div class="col-sm">
{{ form.num_players|as_crispy_field }}
</div>
<div class="col-sm">
{{ form.outcome|as_crispy_field }}
</div>
</div>
</div>
<button type='submit'
class="btn btn-secondary">Submit
</button>
</div>
</div>
</div>
</form>
{% endblock %}