Reputation: 13
i'm new to django.
I have a model that looks like this:
models.py
class Intervention(models.Model):
subject = models.CharField(max_length=200)
begin_date = models.DateField(default=datetime.datetime.today)
end_date = models.DateField(default=datetime.datetime.today)
description = models.TextField(blank=True)
speaker = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
campus = models.ForeignKey(Campus, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Intervention'
verbose_name_plural = 'Interventions'
def __str__(self):
return self.subject
class Evaluation(models.Model):
interventions = models.ForeignKey(Intervention, on_delete=models.CASCADE)
student_id = models.CharField(max_length=20)
speaker_knowledge_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
speaker_teaching_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
speaker_answer_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
slide_content_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
slide_examples_mark = models.IntegerField(validators=[MaxValueValidator(20), MinValueValidator(0)])
comment = models.TextField(blank=True)
The idea is when that the student enter the website on home page he have to choose the campus where he study then he is redirected to a page where he see only interventions of his campus then when he choose the intervention he get the detail of it :
Everything is working so far. Now on "intervention detail page" when the user click on "give mark" he is redirected to a page to create mark (i use CreateView class-based) like below:
Now my question is how can i replace Modelchoicefield in the generated form by pk of the intervetion that student want to give a mark?
Views.py
class CreateEvaluationView(CreateView):
form_class = NewEvaluation
template_name = 'mark.html'
def home(request):
campus = Campus.objects.all().order_by('-name')
return render(request, 'home.html', {'campus': campus})
def mark(request):
if request.method == 'POST':
campus = request.POST.get('campus')
intervention = Intervention.objects.filter(campus=campus)
return render(request, 'mark.html', {'intervention': intervention})
def intervention_detail(request, pk):
intervention_detail = get_object_or_404(Intervention, pk=pk)
return render(request, 'intervention_detail.html', {'intervention_detail': intervention_detail})
urls.py
path('', views.home, name='home'),
path('mark/', views.mark, name='mark'),
path('mark/<int:pk>/', views.intervention_detail, name='intervention_detail'),
path('mark/create/', CreateEvaluationView.as_view(template_name="givemark.html"), name='newmark'),
path('intervention/create/', login_required(CreateInterventionView.as_view(template_name="intervention_create.html")), name='create'),
path('intervention/', login_required(ListInterventionView.as_view(template_name="intervention_list.html")), name='list'),
Thanks in advance for your help! Bast regards.
Upvotes: 1
Views: 2319
Reputation: 3399
You need to provide Intervention
pk to your form. Simplest solution would be to just pass it with url:
path('mark/create/<int:intervention_pk>', CreateEvaluationView.as_view(template_name="givemark.html"), name='newmark'),
It will require modification in your template to pass intervention pk, such as reverse('newmark', intervention_pk=intervention.pk)
Exclude intervention pk from your form, so your users will not be able to modify it accidentally.
class NewEvaluation(forms.ModelForm):
class Meta:
model = Evaluation
exclude = ('interventions',)
Set intervention pk right before you save your form:
class CreateEvaluationView(CreateView):
form_class = NewEvaluation
template_name = 'mark.html'
def form_valid(self, form):
intervention = Intervention.objects.get(pk=self.kwargs['intervention_pk'])
self.object = form.save(commit=False)
self.object.interventions = intervention
self.object.save()
Upvotes: 5