Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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模板:在formset中获取多对多值_Python_Django_Templates_Many To Many_Formset - Fatal编程技术网

Python django模板:在formset中获取多对多值

Python django模板:在formset中获取多对多值,python,django,templates,many-to-many,formset,Python,Django,Templates,Many To Many,Formset,型号: 表格: class Author(models.Model): ... first_name = models.CharField(max_length=30) ... class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) ... booklist.html: class BookForm

型号:

表格:

class Author(models.Model):
   ...
   first_name = models.CharField(max_length=30)
   ...

class Book(models.Model):
   title = models.CharField(max_length=100)
   authors = models.ManyToManyField(Author)
   ...
booklist.html:

class BookForm(ModelForm):
    class Meta:
        model = Book
在书籍页面中加载书籍列表页面的js代码:

def thebooks(request):
  BookFormSet = modelformset_factory(Book, form = BookForm)
  if request.method == 'POST':
    formset = BookFormSet(request.POST)
    if formset.is_valid():
      formset.save()
  else:
    formset = BookFormSet(queryset = Book.objects.all())
  return render_to_response('thebooks.html', {'formset': formset,}, context_instance=RequestContext(request) )

def booklist(request):
  BookFormSet = modelformset_factory(Book, form = BookForm)
  formset = BookFormSet(request.POST)
  return render_to_response('booklist.html', {'formset':formset,}, context_instance=RequestContext(request) )

您可以通过以下编辑形式访问模型实例:

$(document).ready(function() {
      $("#form").submit(function() {
        $.post('/books/booklist/', $(this).serialize(),
          function(data) {
            if (data == "") { alert("No data returned!"); return; }
            // otherwise set the content div to the data we received
            $('#allElements').html(data);
          }
        );
        // disable normal submitting of the form since we use ajax now
        return false;
        });
    });

您可以通过以下编辑形式访问模型实例:

$(document).ready(function() {
      $("#form").submit(function() {
        $.post('/books/booklist/', $(this).serialize(),
          function(data) {
            if (data == "") { alert("No data returned!"); return; }
            // otherwise set the content div to the data we received
            $('#allElements').html(data);
          }
        );
        // disable normal submitting of the form since we use ajax now
        return false;
        });
    });
您可以在模型类上为作者使用unicode方法:

<td>
    {% if form.instance.pk %}{# if this is the form of an existing book #}
        {% for author in form.instance.authors.all %}
            {# then you can iterate over the authors of the book #}
            {{ author.first_name }}
        {% endfor %}
    {% endif %}
</td>
在下拉列表中,只需使用{{form.authors}}。它不再说Author:Model Object或者它通常说的任何东西,而是说John,如果这个人的名字是John

当然,这里的优点是,您也可以直接访问选定的id。

您可以在模型类上为作者使用unicode方法:

<td>
    {% if form.instance.pk %}{# if this is the form of an existing book #}
        {% for author in form.instance.authors.all %}
            {# then you can iterate over the authors of the book #}
            {{ author.first_name }}
        {% endfor %}
    {% endif %}
</td>
在下拉列表中,只需使用{{form.authors}}。它不再说Author:Model Object或者它通常说的任何东西,而是说John,如果这个人的名字是John


当然,这里的优点是,您也可以直接访问选定的id。

谢谢您的回复,但它不起作用……没有错误……什么都没有……只是不显示名称有趣,发布视图和表单python代码,以便我们可以尝试一下。我添加了视图代码。实际上,booklist显示在Books页面内的一个div中。使用js代码。所以作者姓名模板代码在booklist视图中OK我不明白你的问题,我以为你想要该领域的可用作者列表。我用一个如何访问与本书相关的作者的工作示例更新了答案。谢谢你的回复,但它不起作用…没有错误…什么都没有…只是不显示名称有趣,发布视图和表单python代码,以便我们可以尝试一下。我添加了视图代码。实际上,booklist显示在Books页面内的一个div中。使用js代码。所以作者姓名模板代码在booklist视图中OK我不明白你的问题,我以为你想要该领域的可用作者列表。我用一个如何访问与本书相关的作者的工作示例更新了答案。我已经定义了unicode函数…它只格式化了作者在表单集生成的多选择框中的显示方式。我的问题是如何从模板中访问多个字段实值。我已经定义了unicode函数…它只格式化了作者在formset生成的多个选择框中的显示方式。我的问题是如何从模板中访问多个字段的实际值。