Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Createview default value for a foreign key field

I have two classes(tables) schools and students that are related together(foreignkey) When I create a new student I want it to autofill the school field (which is a foreignkey to name field in School class(table) because it is already linked to a school. I have tried def get_initial(self): with no luck (it give me error). If anyone can point out what I am doing wrong…thanks Models.py:

class School(models.Model):
    school_pk = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    principal = models.CharField(max_length=256)
    location = models.CharField(max_length=256)
    def __str__(self):
        return str(self.name)
    def get_absolute_url(self):
        return reverse("basic_app:school_detail",kwargs={'school_pk':self.school_pk})
class Student(models.Model):
    student_pk = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    age = models.PositiveIntegerField()
    school = models.ForeignKey(School,related_name='students',on_delete=models.CASCADE,default=None)

    def __str__(self):
        return self.name
    def get_absolute_url(self):
        return reverse("basic_app:student_detail",kwargs={'student_pk':self.student_pk,'school':self.school})

views.py:

class StudentCreateView(CreateView):
    fields = ("name","age","school")
    model = models.Student
    template_name = 'basic_app/student_form.html'

    pk_url_kwarg = 'student_pk'
    slug_url_kwarg='school'
    def get_initial(self):
        school = get_object_or_404(models.School, school_pk=self.kwargs.get('school_pk'))
        return {
        'school':school,
    }
like image 489
WISAM Avatar asked Dec 11 '22 07:12

WISAM


1 Answers

Since you are storing the school's slug in the URL, it would be better to leave the school field out of the form. Then you can set the school in the form_valid method:

from django.shortcuts import get_object_or_404

class StudentCreateView(CreateView):
    fields = ("name","age",)  # don't include 'school' here
    ...

    def form_valid(self, form):
        school = get_object_or_404(School, slug=self.kwargs['school'])
        form.instance.school = school
        return super(StudentCreateView, self).form_valid(form)
like image 97
Alasdair Avatar answered Dec 28 '22 07:12

Alasdair