0

我想使用表单集来编辑模型“类别”中的所有记录。我想知道如何解决两个问题:

  1. 目前,它使用初始数据填充所有字段,除了“语言”的输入选择和“父”的输入选择。
  2. 它不会在提交时保存。

模型.py

class Category(models.Model):
    ORDER = (
    ('1', '1'),
    ('2', '2'),
    ('3', '3'))
    id = models.AutoField(primary_key=True)
    language = models.ForeignKey(Language, null=False, blank=False)
    name = models.CharField(max_length=256, null=False, blank=False)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    slug = models.SlugField(unique=True, blank=False, null=False)
    order = models.SmallIntegerField(null=True, blank=True, choices=ORDER)
    allow_replies = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now=True)

# Forms
class CategoryForm(ModelForm):
    class Meta:
        model = Category
        fields = ['language', 'name', 'parent', 'allow_replies', 'order']

视图.py

def category_formset(request):
    if is_admin_user(request):
        CategoryFormSet = formset_factory(CategoryForm)
        if request.method == 'POST':
            formset = CategoryFormSet(request.POST)
            if formset.is_valid():
                formset.save()
        else:
            formset = CategoryFormSet(initial=Category.objects.values())
        return render_to_response("pages/category_formset.html",
                                  {"formset": formset, 'is_logged_in': is_logged_in(request)},
                                  context_instance=RequestContext(request))
    else:
        return redirect('/', False)

html模板

<form action="/category/edit/" method="post">
    {% csrf_token %}
    <table>
        <tr>
            <td>Language</td>
            <td>Name</td>
            <td>Parent</td>
            <td>Allow Replies</td>
            <td>Order</td>
        </tr>
        {% for form in formset %}
        <tr>
            {% for item in form %}
                <td>{{ item }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </table>

    <input type="submit" value="Submit" />
</form>

提前致谢。

4

1 回答 1

0

解决方案

模型

# ...
def category_formset(request):
    if is_admin_user(request):
        CategoryFormSet = modelformset_factory(Category, exclude="slug", extra=0)

        if request.POST:
            formset = CategoryFormSet(request.POST)
            if formset.is_valid():
                formset.save()
                return redirect('/categories/')
        else:
            formset = CategoryFormSet(initial=Category.objects.values())
        return render_to_response("pages/category_formset.html",
                                  {"formset": formset, 'is_logged_in': is_logged_in(request)},
                                  context_instance=RequestContext(request))
    else:
        return redirect('/', False)

模板

<form action="/category/edit/" method="post"  enctype="multipart/form-data">
    {% csrf_token %}
    {{ formset.management_form }}
    {{ formset.errors }}
    <table>
        <tr>
            <td>Language</td>
            <td>Name</td>
            <td>Parent</td>
            <td>Allow Replies</td>
            <td>Order</td>
        </tr>
        {% for form in formset %}
        <tr>
            {% for item in form %}
                <td>{{ item }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </table>

    <input type="submit" value="Submit" />
</form>
于 2013-08-11T05:42:44.180 回答