set initial value in CreateView from ForeignKey (non-self.request.user)

后端 未结 1 617
无人及你
无人及你 2020-12-08 08:27

I am attempting to access ForeignKeys in Class Based Views CreateView. I would like to be able to dynamically set initial values in CBV from ForeignKeys and also dynamically

相关标签:
1条回答
  • 2020-12-08 09:20

    You need to instantiate your recipe:

    class IngredientAddView(CreateView):
        model=Ingredient
    
        def get_initial(self):
            recipe = get_object_or_404(Recipe, slug=self.kwargs.get('slug'))
            return {
                'recipe':recipe,
            }
    
    0 讨论(0)
提交回复
热议问题