2017-01-12 7 views
0

新しいグループを追加したいのですが、先生のためにテーブルの中にあるリストから選択する必要があります。

テーブル:教師、グループ
関係:多くのグループ新しいグループを追加するにはどうすればいいですか教師はリストから来る必要があります

models.py

class Teacher(models.Model): 
    name = models.CharField(max_length=100) 
    def __str__(self): 
     return '%s' % (self.name) 

class Group(models.Model): 
    name = models.CharField(max_length=100) 
    teacher = models.ForeignKey(Leraren, on_delete=models.CASCADE) 
    def __str__(self): 
     return '%s %s' % (self.name, self.teacher) 

forms.py

class TeacherForm(forms.Form): 
    name = forms.CharField(required=True, label='name', max_length=100) 

class GroupForm(forms.Form): 
    name = forms.CharField(required=True, label='name', max_length=100) 
    teacher = # choose from list of teachers that are in the db Teachers # 
+0

これを行うには必要性をtheresの、何が起こりますか?何がうまくいかない?あなたは何を期待していますか? – wwii

+0

フォームアクションは投稿であり、リストの名前は先生です。その投稿名の変数を取得するために、forms.pyに何を書かなければならないのですか? – EnzoTrompeneers

答えて

1

から先生1だけ定義フォームとしてあなたはデシベルでリストから選択しようとするとGroupためModelFormは、すべて手動で

class GroupForm(ModelForm): 
    class Meta: 
     model = Group 
+1

ありがとう、それを知らなかった! – EnzoTrompeneers

関連する問題