Python 如何使用视图在Django中保存内联表单集用户字段

Python 如何使用视图在Django中保存内联表单集用户字段,python,django,django-views,overriding,inline-formset,Python,Django,Django Views,Overriding,Inline Formset,我一直在用这篇文章来建立我的网站。我想知道如何将用户字段自动保存到视图中的内联表单集,我使用blockquote对原始表单进行了更改。中的RecipeForm的上下文另请参见下文 self.object = form.save(commit=False) self.object.owner = self.request.user self.object.save() 很好地自动保存,但不是 ingredient_form.owner= self.request.user 我知道Django建

我一直在用这篇文章来建立我的网站。我想知道如何将用户字段自动保存到视图中的内联表单集,我使用blockquote对原始表单进行了更改。中的RecipeForm的上下文另请参见下文

self.object = form.save(commit=False)
self.object.owner = self.request.user
self.object.save()
很好地自动保存,但不是

ingredient_form.owner= self.request.user
我知道Django建议使用,但出于许多不同的原因,大多数人建议在views.py中保存用户字段,而不是表单或模型。如有任何建议或答案,我将不胜感激。以下是完整的代码:

models.py

from django.db import models


class Recipe(models.Model):
    owner = models.ForeignKey(User)
    title = models.CharField(max_length=255)
    description = models.TextField()


class Ingredient(models.Model):
    owner = models.ForeignKey(User)
    recipe = models.ForeignKey(Recipe)
    description = models.CharField(max_length=255)


class Instruction(models.Model):
    recipe = models.ForeignKey(Recipe)
    number = models.PositiveSmallIntegerField()
    description = models.TextField()
forms.py

from django.forms import ModelForm
from django.forms.models import inlineformset_factory 
from .models import Recipe, Ingredient, Instruction


class RecipeForm(ModelForm):
    class Meta:
        model = Recipe
    IngredientFormSet = inlineformset_factory(Recipe, Ingredient)
    InstructionFormSet = inlineformset_factory(Recipe, Instruction)
from django.forms import ModelForm
from .models import Recipe, Ingredient, Instruction


class RecipeForm(ModelForm):

    class Meta:
        model = Recipe
        exclude = ['owner',]

class IngredientForm(ModelForm):

    class Meta:
        model = Ingredient
        exclude = ['owner','recipe',]

class InstructionForm(ModelForm):

    class Meta:
        model = Instruction
        exclude = ['recipe',]
views.py

from django.http import HttpResponseRedirect
from django.views.generic import CreateView  
from .forms import IngredientFormSet, InstructionFormSet, RecipeForm
from .models import Recipe


class RecipeCreateView(CreateView):
    template_name = 'recipe_add.html'
    model = Recipe
    form_class = RecipeForm
    success_url = 'success/'

    def get(self, request, *args, **kwargs):
        """
        Handles GET requests and instantiates blank versions of the form
        and its inline formsets.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet()
        instruction_form = InstructionFormSet()
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests, instantiating a form instance and its inline
        formsets with the passed POST variables and then checking them for
        validity.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet(self.request.POST)
        instruction_form = InstructionFormSet(self.request.POST)
        if (form.is_valid() and ingredient_form.is_valid() and
            instruction_form.is_valid()):
            return self.form_valid(form, ingredient_form, instruction_form)
        else:
            return self.form_invalid(form, ingredient_form, instruction_form)

    def form_valid(self, form, ingredient_form, instruction_form):
        """
        Called if all forms are valid. Creates a Recipe instance along with
        associated Ingredients and Instructions and then redirects to a
        success page.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        self.object.save()
        ingredient_form.instance = self.object
        ingredient_form.owner= self.request.user
        ingredient_form.save()
        instruction_form.instance = self.object
        instruction_form.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, ingredient_form, instruction_form):
        """
        Called if a form is invalid. Re-renders the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))
from .forms import IngredientForm, InstructionForm, RecipeForm


def add_new_value(request):
    rform = RecipeForm(request.POST or None)
    iform = IngredientForm(request.POST or None)
    cform = InstructionForm(request.POST or None)
    if rform.is_valid() and iform.is_valid() and cform.is_valid():
        rinstance = rform.save(commit=False)
        iinstance = iform.save(commit=False)
        cinstance = cform.save(commit=False)
        user = request.user
        rinstance.owner = user
        rinstance.save()
        iinstance.owner = user
        cinstance.owner = user
        iinstance.recipe_id = rinstance.id
        cinstance.recipe_id = rinstance.id
        iinstance.save()
        cinstance.save()
        return HttpResponseRedirect('/admin/')
    context = {
        'rform' : rform,
        'iform' : iform,
        'cform' : cform,
    }
    return render(request, "add_new_recipe.html", context)

我做了一些更多的研究,解决方案看起来有点复杂,遵循这个指南如何添加,但修改如上所述。我意识到为每个模型制作模型表单,然后在视图中链接它们会更简单,因为我在“添加新配方”视图中一次只需要一个子表单,并且可以重用模型表单代码

这是一个新的代码,非常有效!如果您需要更多信息,请随时联系

forms.py

from django.forms import ModelForm
from django.forms.models import inlineformset_factory 
from .models import Recipe, Ingredient, Instruction


class RecipeForm(ModelForm):
    class Meta:
        model = Recipe
    IngredientFormSet = inlineformset_factory(Recipe, Ingredient)
    InstructionFormSet = inlineformset_factory(Recipe, Instruction)
from django.forms import ModelForm
from .models import Recipe, Ingredient, Instruction


class RecipeForm(ModelForm):

    class Meta:
        model = Recipe
        exclude = ['owner',]

class IngredientForm(ModelForm):

    class Meta:
        model = Ingredient
        exclude = ['owner','recipe',]

class InstructionForm(ModelForm):

    class Meta:
        model = Instruction
        exclude = ['recipe',]
views.py

from django.http import HttpResponseRedirect
from django.views.generic import CreateView  
from .forms import IngredientFormSet, InstructionFormSet, RecipeForm
from .models import Recipe


class RecipeCreateView(CreateView):
    template_name = 'recipe_add.html'
    model = Recipe
    form_class = RecipeForm
    success_url = 'success/'

    def get(self, request, *args, **kwargs):
        """
        Handles GET requests and instantiates blank versions of the form
        and its inline formsets.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet()
        instruction_form = InstructionFormSet()
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests, instantiating a form instance and its inline
        formsets with the passed POST variables and then checking them for
        validity.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet(self.request.POST)
        instruction_form = InstructionFormSet(self.request.POST)
        if (form.is_valid() and ingredient_form.is_valid() and
            instruction_form.is_valid()):
            return self.form_valid(form, ingredient_form, instruction_form)
        else:
            return self.form_invalid(form, ingredient_form, instruction_form)

    def form_valid(self, form, ingredient_form, instruction_form):
        """
        Called if all forms are valid. Creates a Recipe instance along with
        associated Ingredients and Instructions and then redirects to a
        success page.
        """
        self.object = form.save(commit=False)
        self.object.owner = self.request.user
        self.object.save()
        ingredient_form.instance = self.object
        ingredient_form.owner= self.request.user
        ingredient_form.save()
        instruction_form.instance = self.object
        instruction_form.save()
        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, ingredient_form, instruction_form):
        """
        Called if a form is invalid. Re-renders the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))
from .forms import IngredientForm, InstructionForm, RecipeForm


def add_new_value(request):
    rform = RecipeForm(request.POST or None)
    iform = IngredientForm(request.POST or None)
    cform = InstructionForm(request.POST or None)
    if rform.is_valid() and iform.is_valid() and cform.is_valid():
        rinstance = rform.save(commit=False)
        iinstance = iform.save(commit=False)
        cinstance = cform.save(commit=False)
        user = request.user
        rinstance.owner = user
        rinstance.save()
        iinstance.owner = user
        cinstance.owner = user
        iinstance.recipe_id = rinstance.id
        cinstance.recipe_id = rinstance.id
        iinstance.save()
        cinstance.save()
        return HttpResponseRedirect('/admin/')
    context = {
        'rform' : rform,
        'iform' : iform,
        'cform' : cform,
    }
    return render(request, "add_new_recipe.html", context)
模板:添加新配方.html

<!DOCTYPE html>
<html>
<head>
    <title>Add Recipe</title>
</head>

<body>
    <div>
        <h1>Add Recipe</h1>
        <form action="" method="post">
            {% csrf_token %}
            <div>
                {{ rform.as_p }}
                {{ iform.as_p }}
                {{ cform.as_p }}
            </div>
            <input type="submit" value="Add recipe" class="submit" />
        </form>
    </div>
</body>
</html>

很高兴我找到了这个帖子。我也尝试使用内联表单集和通用CreateView来实现这一点。它正在工作,但我不知道如何将子实例的所有者设置为发出请求的用户。这是一个很好的例子,与内联表单集相比,使用基于函数的视图和模型表单要容易得多。谢谢你发布你的解决方案,它帮了我很多。如何添加成分或编辑成分?