Limit values in the modelformset field

前端 未结 2 1960
别跟我提以往
别跟我提以往 2021-01-28 00:06

I\'ve been trying to solve this problem for a couple of days now, getting quite desperate. See the commented out code snippets for some of the things I\'ve tried but didn\'t wor

相关标签:
2条回答
  • 2021-01-28 00:49
    Category.objects.filter(user=request.user)
    

    returns a list object for the initial value in your form which makes little sense.

    Try instead

    Category.objects.get(user=request.user)
    

    or

    Category.objects.filter(user=request.user)[0]
    
    0 讨论(0)
  • 2021-01-28 00:52

    You don't need any kind of custom forms. You can change the queryset of category field as:

    IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category'))
    IngrFormSet.form.base_fields['category'].queryset = Category.objects.filter(user__id=request.user.id)
    
    0 讨论(0)
提交回复
热议问题