'Passing Data into Forms Django
Hypothetically;
I have a simple system that has a list of choices on a screen. Each item has a button so you can add multiple supplementary items/information alongside the item selected but not change the item selected. Each item has a unique id which I want to pass into the form so that the comment is always associated with the item. I have a model that contains a foreign key that links the comment to the item and the Modelform works but I have to select the item from a drop down and I would like this information to be pre-populated in the form. This seems so simple but I have been going round in circles for days. Any pointers would be greatly appreciated
This is the Model for the 'comments'
class roundscore(models.Model):
scoreid = models.ForeignKey('Score', on_delete=CASCADE)
shot_1 = models.IntegerField(default=0, blank=True, null=True)
shot_2 = models.IntegerField(default=0, blank=True, null=True)
shot_3 = models.IntegerField(default=0, blank=True, null=True)
shot_4 = models.IntegerField(default=0, blank=True, null=True)
shot_5 = models.IntegerField(default=0, blank=True, null=True)
shot_6 = models.IntegerField(default=0, blank=True, null=True)
end_1 = models.IntegerField(default=0, blank=True, null=True)
tens = models.IntegerField(default=0, blank=True, null=True)
This is the form
class AddRoundScore(forms.ModelForm):
class Meta:
model = roundscore
fields = [
'shot_1',
'shot_2',
'shot_3',
'shot_4',
'shot_5',
'shot_6',
'end_1',
'tens',
'scoreid'
]
This is the view
def addscorecard (request, pk):
posts = get_object_or_404(Score, pk=pk)
post = Score.objects.values('id', 'rndname__roundname').filter(id = pk)
print('Post', post)
print('Request', type(post))
if request.method == 'POST':
form = AddRoundScore(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.scoreid = post
post.save()
print('Errors', form.errors.as_data())
return redirect('readscorecard', post.pk)
else:
form = AddRoundScore()
print('Errors', form.errors.as_data())
return render(request, 'score/addscorecard.html', {'form': form, 'posts': posts, 'post': post})
The scoreid field links to a table where the score is recorded. I have tried adding the pk to the function in order to pass it on to the score id rather than picking it out of a list when entering the shot values, this could lead to errors, picking the wrong scoreid. I cannot use the name associated with the scoreid as this could be the same if the same round is attempted. All I want to do is pre-populate the scoreid field with the value taken from the button on the scores page
<td><h5><a class="btn btn-outline-dark btn-block" href="{% url 'score:addscorecard' post.id %}">Add Score</a></h5></td>
<td><h5><a class="btn btn-outline-dark btn-block" href="{% url 'score:readscorecard' post.id %}">Read Score</a></h5></td>
For completeness here is the url file
path("addscorecard/<int:pk>", views.addscorecard, name="addscorecard"),
I am sure the solution is quiet simple but I cannot get to the bottom of it
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
