English 中文(简体)
两种模式的一种形式
原标题:One form for two models

<>>>>> 这个问题已经解决,你能够看到的所有法典都行之有效。

Hello!

我在文本处理和段落之间有外在关系,我的目标是使文本处理前端形成/编辑形式,就像在《示范公约》中那样:文本处理的几个领域,然后是一小段。 问题在于,我对如何证实和拯救:

@login_required
def textpage_add(request):
    profile = request.user.profile_set.all()[0]
    if not (profile.is_admin() or profile.is_editor()):
        raise Http404
    PageFormSet = inlineformset_factory(TextPage, Paragraph, fields=( title ,  text , ), extra=5)
    textpage = TextPage()
    if request.POST:
        textpageform = TextPageForm(request.POST, instance=textpage, prefix= page )
        formset = PageFormSet(request.POST, instance=textpage, prefix= paragraphs )
        # Saving data
        if textpageform.is_valid():
            textpageform.save()
            if formset.is_valid():
                formset.save()
            return HttpResponseRedirect(reverse(consult_categories))
    else:
        textpageform = TextPageForm(instance=textpage, prefix= page )
        formset = PageFormSet(instance=textpage, prefix= paragraphs )
    return render_to_response( textpages/manage.html , {  formset  : formset,
                                                          textpageform  : textpageform,
                                              }, context_instance=RequestContext(request))

我知道,你甚至不希望工作,而是想显示我想要完成的工作。 这里是模型的相关部分。 py:

class TextPage(models.Model):
    title = models.CharField(max_length=100)
    page_sub_category = models.ForeignKey(PageSubCategory, blank=True, null=True)

    def __unicode__(self):
        return self.title

class Paragraph(models.Model):
    article = models.ForeignKey(TextPage)
    title = models.CharField(max_length=100, blank=True, null=True)
    text = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return self.title

希望得到任何帮助。 感谢!

<>UPDATE。 实例说明增加了,但还是做了一些工作,结果导致对这一指示进行验证:

formset = PageFormSet(request.POST, instance=textpage, prefix= paragraphs ) 

任何想法?

最佳回答

更新的法典,如参考文件,实际上会奏效! 问题在模板中:我放弃管理。 模板代码如下:

{% extends "site_base.html" %}
{% block body %}
<form action="" method="post">
{{ textpageform.as_p }}
{{ formset.management_form }}
{% for form in formset.forms %}
    <p>{{ form.as_p }}</p>
{% endfor %}
<input type="submit" value="Go" />
{% endblock %}

希望这一榜样有助于像我这样的新方:

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签