Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django在保存新对象之前添加主键_Python_Django_Django Models_Django Forms - Fatal编程技术网

Python Django在保存新对象之前添加主键

Python Django在保存新对象之前添加主键,python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我是Django的新手,有一个问答项目。对于每个问题,您可能有多个已经存在的标签或放置新标签。应在保存问题之前创建新标记。我如何正确地解决这个问题?到目前为止,我已经: def question_add(request): # redirect user to login page if not authenticated if not request.user.is_authenticated(): return render(request, 'account

我是Django的新手,有一个问答项目。对于每个问题,您可能有多个已经存在的标签或放置新标签。应在保存问题之前创建新标记。我如何正确地解决这个问题?到目前为止,我已经:

def question_add(request):
    # redirect user to login page if not authenticated
    if not request.user.is_authenticated():
        return render(request, 'account/login.html')

    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = QuestionForm(request.POST)

        if form.is_valid():
            # process the data in form.cleaned_data as required
            instance = form.save(commit=False)
            instance.created_by = request.user
            instance.save()

            messages.success(request, 'Question added with success.')
            # redirect to the main page:
            return HttpResponseRedirect('/')
        else:
            messages.warning(request, 'Please correct the errors.')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = QuestionForm()

    return render(request, 'question/add.html', {'form': form})

这应该在表单之前完成。它是有效的()还是存在一个神奇的方法来实现这一点?

我假设
标签是问题模型的许多字段

表单内。是否有效()
以添加多个字段数据

if form.is_valid():
     instance = form.save(commit=False)
     instance.created_by = request.user
     instance.save()
     # returns the list of tag names
     tags = request.POST.get('tags')
     for tag in tags:
         # create if tag not found with given name or return existing tag
         obj, created = Tag.objects.get_or_create(name=tag)
         instance.tags.add(obj)
如果标记为ForiegnKey:

if form.is_valid():
     instance = form.save(commit=False)
     instance.created_by = request.user
     tag_name = request.POST.get('tag')
     obj, created = Tag.objects.get_or_create(name=tag_name)
     instance.tag = obj.id
     instance.save()

在您的
表单中尝试。在两个
函数之间,是否有效()
。save()
函数。旁注:要重定向未经身份验证的用户,您可以使用登录所需的装饰程序:@AdamStarrh:您有示例吗?thxtag是一个多领域的问题模型。但我总是收到错误消息,似乎表单无效。只使用已经存在的标签Everything工作得非常完美。添加新标记时,我会收到错误消息…是否发布特定错误?您的表单可能正在尝试验证标记是否作为对象存在于数据库中。你可能需要解决这个问题。