Python Django框架:使用save_m2m()方法在表单中保存多个字段会引发错误

Python Django框架:使用save_m2m()方法在表单中保存多个字段会引发错误,python,django,many-to-many,Python,Django,Many To Many,我正在尝试用django构建一个食谱web应用程序。你可以在几本烹饪书中创建食谱。这就是为什么“烹饪书”是一个众多的领域。 通过创建一个食谱,它会显示多个复选框,您可以在这些复选框中查看多个食谱。 保存此项会引发一个错误,提示: ValueError: invalid literal for int() with base 10: 'a' 在myviews.py中: def create_recipe(request): if not request.user.is_authentic

我正在尝试用django构建一个食谱web应用程序。你可以在几本烹饪书中创建食谱。这就是为什么“烹饪书”是一个众多的领域。 通过创建一个食谱,它会显示多个复选框,您可以在这些复选框中查看多个食谱。 保存此项会引发一个错误,提示:

ValueError: invalid literal for int() with base 10: 'a'
在my
views.py
中:

def create_recipe(request):
    if not request.user.is_authenticated():
        return render(request, 'cookbook/login.html')
    else:
        form = RecipeForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            recipe = form.save(commit=False)
            recipe.user = request.user
            recipe.picture = request.FILES['picture']
            file_type = recipe.picture.url.split('.')[-1]
            file_type = file_type.lower()
            if file_type not in IMAGE_FILE_TYPES:
                context = {
                    'recipe': recipe,
                    'form': form,
                    'error_message': 'Image file must be PNG, JPG, or JPEG',
                }
                return render(request, 'cookbook/create_recipe.html', context)
            recipe.save()
            form.save_m2m()
            cookbook = Cookbook.objects.filter(user=request.user)
            return render(request, 'cookbook/index.html', {'cookbook': cookbook})
        context = {
            "form": form,
        }
        return render(request, 'cookbook/create_recipe.html', context)
class RecipeForm(forms.ModelForm):
    directions = forms.CharField(widget=forms.Textarea)
    ingredients = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Recipe
        fields = ['title', 'ingredients', 'directions', 'picture', 'cookbooks']

    def __init__ (self, *args, **kwargs):
        super(RecipeForm, self).__init__(*args, **kwargs)
        self.fields["cookbooks"].widget = forms.widgets.CheckboxSelectMultiple()
        self.fields["cookbooks"].help_text = ""
        self.fields["cookbooks"].queryset = Cookbook.objects.all()
在my
forms.py
中:

def create_recipe(request):
    if not request.user.is_authenticated():
        return render(request, 'cookbook/login.html')
    else:
        form = RecipeForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            recipe = form.save(commit=False)
            recipe.user = request.user
            recipe.picture = request.FILES['picture']
            file_type = recipe.picture.url.split('.')[-1]
            file_type = file_type.lower()
            if file_type not in IMAGE_FILE_TYPES:
                context = {
                    'recipe': recipe,
                    'form': form,
                    'error_message': 'Image file must be PNG, JPG, or JPEG',
                }
                return render(request, 'cookbook/create_recipe.html', context)
            recipe.save()
            form.save_m2m()
            cookbook = Cookbook.objects.filter(user=request.user)
            return render(request, 'cookbook/index.html', {'cookbook': cookbook})
        context = {
            "form": form,
        }
        return render(request, 'cookbook/create_recipe.html', context)
class RecipeForm(forms.ModelForm):
    directions = forms.CharField(widget=forms.Textarea)
    ingredients = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Recipe
        fields = ['title', 'ingredients', 'directions', 'picture', 'cookbooks']

    def __init__ (self, *args, **kwargs):
        super(RecipeForm, self).__init__(*args, **kwargs)
        self.fields["cookbooks"].widget = forms.widgets.CheckboxSelectMultiple()
        self.fields["cookbooks"].help_text = ""
        self.fields["cookbooks"].queryset = Cookbook.objects.all()

如果您收到错误,您应该发布准确的消息和完整的回溯。您可以发布模型吗??