Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
Django:UpdateView用于双嵌套表单_Django_Nested Forms_Django Class Based Views - Fatal编程技术网

Django:UpdateView用于双嵌套表单

Django:UpdateView用于双嵌套表单,django,nested-forms,django-class-based-views,Django,Nested Forms,Django Class Based Views,我目前在为双嵌套表单编写UpdateView时出错。我的架构由一个包含一个表单和两个表单集的模板组成。出版商有一个大表格。然后,用户可以向每个作者添加任意多的作者表单集,也可以向每个作者添加任意多的书籍表单集。所以它看起来像: AuthorInlineFormSet = inlineformset_factory( Publisher, Author, fields="__all__", extra=1, can_delete=Fal

我目前在为双嵌套表单编写UpdateView时出错。我的架构由一个包含一个表单和两个表单集的模板组成。出版商有一个大表格。然后,用户可以向每个作者添加任意多的作者表单集,也可以向每个作者添加任意多的书籍表单集。所以它看起来像:

AuthorInlineFormSet = inlineformset_factory(
    Publisher,
    Author,
    fields="__all__",
    extra=1, 
    can_delete=False,
)

BookInlineFormSet = inlineformset_factory(
    Author,
    Book,
    fields="__all__",
    extra=1,
    can_delete=False,
)
class FormCreateView(CreateView):
    model = Publisher
    template_name = 'reservations/create.html'
    fields = "__all__"

    def form_valid(self, form):
        result = super(FormCreateView, self).form_valid(form)

        authors_count = 0
        authors_formset = AuthorInlineFormSet(form.data, instance=self.object, prefix='authors_formset')
        if authors_formset.is_valid():
            authors = authors_formset.save()

            for author in authors:
                books_formset = BookInlineFormSet(form.data, instance=author, prefix='books_formset_%s' % authors_count)
                if books_formset.is_valid():
                    books_formset.save()
                authors_count += 1
        return result

    def get_context_data(self, **kwargs):
        context = super(FormCreateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(prefix='authors_formset')
        context['books_formset'] = BookInlineFormSet(prefix='books_formset_0')
        return context

class FormUpdateView(UpdateView):
    model = Publisher 
    template_name = 'reservations/create.html'
    fields = "__all__"

    def get_success_url(self):
        return reverse_lazy('reservations:dashboard')

    def get_object(self, *kargs, **kwargs):
        request = get_object_or_404(Publisher, pk=self.kwargs['pk'])

        return request

    def get_context_data(self, **kwargs):
        context = super(FormUpdateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_count = 0
        authors_formset = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_formset = authors_formset.queryset
        for author in authors_formset:
            context['books_formset'] = BookInlineFormSet(instance=author, prefix='books_formset_%s' % authors_count)
        return context 
  • Publisher->Author1,Author2,Author3
  • Author1->Book1,Book2,Book3
  • 作者2->Book4
  • 作者3->Book5,Book6
同样,这些都列在一个create.html模板中。我用CreateView写的,效果很好。现在写UpdateView, 我重定向到我的html模板以创建表单,并且可以使用publisher和author的数据正确填充表单。但是,当我尝试用图书数据填充表单时,它会用相同的图书实例填充每个author实例,而不是用匹配的图书实例填充每个author实例。所以它看起来像:

AuthorInlineFormSet = inlineformset_factory(
    Publisher,
    Author,
    fields="__all__",
    extra=1, 
    can_delete=False,
)

BookInlineFormSet = inlineformset_factory(
    Author,
    Book,
    fields="__all__",
    extra=1,
    can_delete=False,
)
class FormCreateView(CreateView):
    model = Publisher
    template_name = 'reservations/create.html'
    fields = "__all__"

    def form_valid(self, form):
        result = super(FormCreateView, self).form_valid(form)

        authors_count = 0
        authors_formset = AuthorInlineFormSet(form.data, instance=self.object, prefix='authors_formset')
        if authors_formset.is_valid():
            authors = authors_formset.save()

            for author in authors:
                books_formset = BookInlineFormSet(form.data, instance=author, prefix='books_formset_%s' % authors_count)
                if books_formset.is_valid():
                    books_formset.save()
                authors_count += 1
        return result

    def get_context_data(self, **kwargs):
        context = super(FormCreateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(prefix='authors_formset')
        context['books_formset'] = BookInlineFormSet(prefix='books_formset_0')
        return context

class FormUpdateView(UpdateView):
    model = Publisher 
    template_name = 'reservations/create.html'
    fields = "__all__"

    def get_success_url(self):
        return reverse_lazy('reservations:dashboard')

    def get_object(self, *kargs, **kwargs):
        request = get_object_or_404(Publisher, pk=self.kwargs['pk'])

        return request

    def get_context_data(self, **kwargs):
        context = super(FormUpdateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_count = 0
        authors_formset = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_formset = authors_formset.queryset
        for author in authors_formset:
            context['books_formset'] = BookInlineFormSet(instance=author, prefix='books_formset_%s' % authors_count)
        return context 
  • Publisher->Author1,Author2,Author3
  • Author1->Book1,Book2,Book3
  • Author2->Book1,Book2,Book3
  • Author3->Book1,Book2,Book3
My models.py看起来像:

class Publisher(models.Model):
    name = models.CharField(max_length=255, unique=True)

class Author(models.Model):
    name = models.CharField(max_length=255, unique=True) 
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)

class Book(models.Model):
    title = models.CharField(max_length=255, unique=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE) 
My forms.py看起来像:

AuthorInlineFormSet = inlineformset_factory(
    Publisher,
    Author,
    fields="__all__",
    extra=1, 
    can_delete=False,
)

BookInlineFormSet = inlineformset_factory(
    Author,
    Book,
    fields="__all__",
    extra=1,
    can_delete=False,
)
class FormCreateView(CreateView):
    model = Publisher
    template_name = 'reservations/create.html'
    fields = "__all__"

    def form_valid(self, form):
        result = super(FormCreateView, self).form_valid(form)

        authors_count = 0
        authors_formset = AuthorInlineFormSet(form.data, instance=self.object, prefix='authors_formset')
        if authors_formset.is_valid():
            authors = authors_formset.save()

            for author in authors:
                books_formset = BookInlineFormSet(form.data, instance=author, prefix='books_formset_%s' % authors_count)
                if books_formset.is_valid():
                    books_formset.save()
                authors_count += 1
        return result

    def get_context_data(self, **kwargs):
        context = super(FormCreateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(prefix='authors_formset')
        context['books_formset'] = BookInlineFormSet(prefix='books_formset_0')
        return context

class FormUpdateView(UpdateView):
    model = Publisher 
    template_name = 'reservations/create.html'
    fields = "__all__"

    def get_success_url(self):
        return reverse_lazy('reservations:dashboard')

    def get_object(self, *kargs, **kwargs):
        request = get_object_or_404(Publisher, pk=self.kwargs['pk'])

        return request

    def get_context_data(self, **kwargs):
        context = super(FormUpdateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_count = 0
        authors_formset = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_formset = authors_formset.queryset
        for author in authors_formset:
            context['books_formset'] = BookInlineFormSet(instance=author, prefix='books_formset_%s' % authors_count)
        return context 
My views.py看起来像:

AuthorInlineFormSet = inlineformset_factory(
    Publisher,
    Author,
    fields="__all__",
    extra=1, 
    can_delete=False,
)

BookInlineFormSet = inlineformset_factory(
    Author,
    Book,
    fields="__all__",
    extra=1,
    can_delete=False,
)
class FormCreateView(CreateView):
    model = Publisher
    template_name = 'reservations/create.html'
    fields = "__all__"

    def form_valid(self, form):
        result = super(FormCreateView, self).form_valid(form)

        authors_count = 0
        authors_formset = AuthorInlineFormSet(form.data, instance=self.object, prefix='authors_formset')
        if authors_formset.is_valid():
            authors = authors_formset.save()

            for author in authors:
                books_formset = BookInlineFormSet(form.data, instance=author, prefix='books_formset_%s' % authors_count)
                if books_formset.is_valid():
                    books_formset.save()
                authors_count += 1
        return result

    def get_context_data(self, **kwargs):
        context = super(FormCreateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(prefix='authors_formset')
        context['books_formset'] = BookInlineFormSet(prefix='books_formset_0')
        return context

class FormUpdateView(UpdateView):
    model = Publisher 
    template_name = 'reservations/create.html'
    fields = "__all__"

    def get_success_url(self):
        return reverse_lazy('reservations:dashboard')

    def get_object(self, *kargs, **kwargs):
        request = get_object_or_404(Publisher, pk=self.kwargs['pk'])

        return request

    def get_context_data(self, **kwargs):
        context = super(FormUpdateView, self).get_context_data(**kwargs)
        context['authors_formset'] = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_count = 0
        authors_formset = AuthorInlineFormSet(instance=self.object, prefix='authors_formset')
        authors_formset = authors_formset.queryset
        for author in authors_formset:
            context['books_formset'] = BookInlineFormSet(instance=author, prefix='books_formset_%s' % authors_count)
        return context 
现在,如果我要打印UpdateView的get_context_数据的for循环中的context['books_formset',它会正确地打印出每个作者的匹配书籍表单集,所以它会打印出来(Book5,Book6),(Book4),然后(Book1,Book2,Book3)。因此,我知道它可以正确地为每个作者实例找到相应的书籍实例。但是,它会将所有书籍实例写入Book1、Book2、Book3,这样每个作者都有Book1、Book2、Book3。我不太确定如何解决这个问题,如果有任何帮助,我将不胜感激!非常感谢你