我的代码非常简单直接。这些是我的表单
class LearningObjectiveForm( ModelForm ):
class Meta:
model = LearningObjective
fields = ( 'learning_objective', 'topic', )
这是我的模板
的摘录<form method="POST" action=".">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Speichern"/>
</form>
此表单现在可以为每个用户访问每个主题。由于此字段通常具有用户限制,因此我不希望显示每个主题,而只是针对特定用户的主题。我的模型中针对此特定情况的结构如下所示
class Trainee( models.Model ):
user = models.OneToOneField( User )
first_name = models.TextField()
last_name = models.TextField()
education_beginning = models.DateField()
comment = models.TextField( null = True, blank = True )
class Topic( models.Model ):
trainee = models.ForeignKey( Trainee )
topic = models.TextField()
class LearningObjective( models.Model ):
trainee = models.ForeignKey( Trainee )
topic = models.ManyToManyField( Topic, blank = True, null = True)
learning_objective = models.TextField()
我的观看次数
class LearningObjectiveView( LoginRequiredMixin, FormView ):
form_class = LearningObjectiveForm( user = request.user )
success_url = reverse_lazy( 'learning_objective' )
template_name = 'learningobjective.html'
def get_context_data( self, **kwargs ):
trainee = Trainee.objects.get( user = self.request.user )
context = super( LearningObjectiveView, self ).get_context_data( **kwargs )
context['learningobjective'] = LearningObjective.objects.filter( trainee = trainee.id )
context['topic'] = Topic.objects.filter( trainee = trainee.id )
return context
def form_valid( self, form ):
self.object = form.save( commit = False )
if self.request.user.is_authenticated():
self.object.trainee = Trainee.objects.get( user = self.request.user )
self.object.save()
return super( LearningObjectiveView, self ).form_valid( form )
主题应该是受训者特定的。我是否必须在模板中编写自己的表单,或者是否有这样的方式以便我仍然可以自动生成它?
答案 0 :(得分:1)
您可以在init函数中修改ModelChoiceField的queryset属性,这将限制选择并正确验证。
表单看起来类似于以下代码
class LearningObjectiveForm(ModelForm):
class Meta:
model = LearningObjective
fields = ('learning_objective', 'topic',)
def __init__(self, user, *args, **kwargs):
super(LearningObjectiveForm,self ).__init__(*args,**kwargs)
self.fields['topic'].queryset = Topic.objects.filter(trainee=user)
然后在表单中确保使用用户LearningObjectiveForm(user=request.user)
答案 1 :(得分:0)
除了awwester的答案外,我还将其添加到视图
中class LearningObjectiveView( LoginRequiredMixin, FormView ):
form_class = LearningObjectiveForm
template_name = 'learningobjective.html'
success_url = reverse_lazy( 'learning_objective' )
def get_context_data( self, **kwargs ):
trainee = Trainee.objects.get( user = self.request.user )
context = super( LearningObjectiveView, self ).get_context_data( **kwargs )
context['learningobjective'] = LearningObjective.objects.filter( trainee = trainee.id )
context['topic'] = Topic.objects.filter( trainee = trainee.id )
return context
def get_form_kwargs( self ):
kwargs = super( LearningObjectiveView , self ).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid( self, form ):
self.object = form.save( commit = False )
if self.request.user.is_authenticated():
self.object.trainee = Trainee.objects.get( user = self.request.user )
self.object.save()
return super( LearningObjectiveView, self ).form_valid( form )
get_form_kwargs
将当前用户的kwarg用户传递给表单。
然后我的表格看起来像这样:
class LearningObjectiveForm( ModelForm ):
def __init__( self, user, *args, **kwargs ):
trainee = Trainee.objects.get( user = user )
super( LearningObjectiveForm, self ).__init__( *args, **kwargs )
self.fields['topic'].queryset = Topic.objects.filter( trainee = trainee.id )
class Meta:
model = LearningObjective
fields = ( 'learning_objective', 'topic', )
现在工作正常,但如果有些事情多余或不必要,请告诉我:)