I've been building upon the basic polls project to create an application for my band. The goal is to have each member vote on a gig date request, and then for each member that has voted stating they're available, a nametag is then created which can be added to a gig card along with other information for the gig.
I have the nametags being generated within the admin panel when each user votes in favour of the date. I have the gig cards being generated with all relevant information. If I go into the admin panel, I can select one, or many name tags and they will display on the cards as desired.. however, I would like for this to be automated from a views function to save manual selection. In the code, I've toyed with a set to collate all the users that voted 'yes'.
I've searched for days and tried various different things but being new to django, (and programming) it's become quite overwhelming. I feel like it's probably something quite simple, but my lack of broader understanding has led me here for my first stack question. Thanks in advance and apologies if there are holes in my explanation. Here's the relevant code:
models.py
class ConfirmedGigs(models.Model):
request = models.ForeignKey(
Question, null=True, blank=True, on_delete=models.CASCADE)
venue = models.CharField(max_length=100, null=True, blank=True)
fee = models.CharField(max_length=10, null=True, blank=True)
set_type = models.CharField(max_length=50, null=True, blank=True)
additional_info = models.TextField(max_length=200, null=True, blank=True)
tags = models.ManyToManyField('NameTag', blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True,
primary_key=True, editable=False)
def __str__(self):
return self.venue
class NameTag(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True,
primary_key=True, editable=False)
def __str__(self):
return self.name
views.py
@login_required(login_url="/")
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
if Vote.objects.filter(poll_id=question_id, user_id=request.user.id).exists():
return render(request, "polls/detail.html", {"question": question, "error_message": "You have already voted on this poll"})
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
return render(
request,
"polls/detail.html",
{
"question": question,
"error_message": "You didn't select a choice.",
},
)
else:
selected_choice.votes = F("votes") + 1
selected_choice.save()
Vote.objects.create(poll_id=question_id,
user_id=request.user.id, choice=selected_choice)
if str(selected_choice) == "Y":
NameTag.objects.create(name=request.user)
createGigCard(question_id)
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
def createGigCard(question_id):
question = get_object_or_404(Question, pk=question_id)
voters = NameTag.objects.all()
vote_set = set()
for vote in voters:
name = str(vote)
vote_set.add(name)
if ConfirmedGigs.objects.filter(
request=question, venue="TBC", fee="TBC", set_type="TBC", additional_info="PENDING AVAILABILITY CONFIRMATION").exists():
pass
else:
ConfirmedGigs.objects.create(
request=question, venue="TBC", fee="TBC", set_type="TBC", additional_info="PENDING AVAILABILITY CONFIRMATION")
I thought that perhaps I could add in tags to the creation method like this:
ConfirmedGigs.objects.create( request=question, venue="TBC", fee="TBC", set_type="TBC", additional_info="PENDING AVAILABILITY CONFIRMATION", tags=vote_set)
or even tags= "name", but that presents me with this error which I've been unable to make sense of..
"Direct assignment to the forward side of a many-to-many set is prohibited. Use tags.set() instead."
Desired outcome out look like this: