以django形式显示模型值(ModelChoiceField)

时间:2016-09-15 13:40:28

标签: django-forms django-templates

我是Django的新手并且很难理解整个Django表单。我正在创建一个获取用户输入的表单。用户可以添加更多行(javascript)。我有几个问题。

1。如何使用queryset向用户显示组合选项?目前我正在获得一个带有对象的组合框,而不是它的原始值

我需要显示item_combo值



class NameForm(forms.Form):
    # your_name = forms.CharField(label='Your name', max_length=100)
    new_date = forms.DateField(initial=datetime.date.today, required=False, error_messages={'required': 'Your Name is Required'})
    item_combo = forms.ModelChoiceField(Item.objects.all())
    new_parc = forms.CharField(max_length=200, required=False)
    new_vid = forms.CharField(max_length=50, required=False)



 views.py



def get_name(request):
    if request.method == 'POST':
        form = NameForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            logger.info('FORM INFO')
            logger.info('form info date is %s ', data)
            new_date = form.cleaned_data['new_date']
            new_parc = form.cleaned_data['new_date']
            item_combo = form.cleaned_data['item_combo']
            return HttpResponseRedirect('/thanks/')
        else:
            logger.error('Form is invalid. Errors are %s', form.errors)
    else:
        form = NameForm()
        logger.error('Form has GET request. Errors are %s', form.errors)

    return render(request, 'form.html', {'form': form})




form.html



<html>
{% load staticfiles %}
{% load static %}
{% include 'head.html' %}

 <h1>New post</h1>


<form action="your-name" method="post">
    {% csrf_token %}
    <div class="fieldWrapper">

        <label for="{{ form.new_date.id_for_label }}">Date</label>
        {{ form.new_date }}
    </div>
    <div class="fieldWrapper">

        <label for="{{ form.new_date.id_for_label }}">Voucher ID</label>
        {{ form.new_vid }}
    </div>
    <div class="fieldWrapper">

        <label for="{{ form.new_date.id_for_label }}">Particulars</label>
        {{ form.new_parc }}
    </div>
    <div class="fieldWrapper">

        <label for="{{ form.item_combo.id_for_label }}">Items</label>
        {{ form.item_combo }}
    </div>


    <div class="fieldWrapper">

        <label for="{{ form.item_combo.id_for_label }}">Burgers</label>
        {{ form.burger_combo }}
    </div>
    <table>
        <thead style="background-color:#9df0e0;color: #73879C">
        <tr class="headings">
            <th>
                <input type="checkbox" id="check-all" class="flat">
            </th>
            <th class="column-title">Sr. No</th>
            <th class="column-title">Code</th>
            <th class="column-title">Particulars</th>
            <th class="column-title">Qty</th>
            <th class="column-title">Rate</th>
            <th class="column-title">Amount</th>
            <th class="bulk-actions" colspan="7">
                <a class="antoo" style="color:#fff; font-weight:500;">Bulk
                    Actions (
                    <span class="action-cnt"> </span> ) <i
                            class="fa fa-chevron-down"></i></a>
            </th>
        </tr>
        </thead>

        <tbody>
        <tr class="even pointer" id='addr0'>
            <td class="a-center ">
                <input type="checkbox" class="flat" name="check0">
            </td>
            <td><input type="hidden" name="c0">1</td>
            <td>
                <select class="select2_single form-control" tabindex="-1" name="scode0">
                    <option value="">Select One</option>
                    {% for item in items %}
                    <option value="{{ item }}">{{ item }}</option>
                    {% endfor %}
                </select>
            </td>
            <td class=" "><input type="text" name='part0' placeholder='Add particulars' class="form-control"/></td>
            <td class=" "><input type="text" name='qty0' placeholder='Add Quantity ' class="form-control"/></td>
            <td class=" "><input type="text" name='rate0' placeholder='Add rate' class="form-control"/></td>
            <td class=" "><input type="text" name='am0' placeholder='Add amount' class="form-control"/></td>
        </tr>
        </tbody>
    </table>
    <input type="submit" value="Add more rows"/>
    <input type="submit" value="Delete rows"/>
    <input type="submit" value="Submit"/>
</form>


</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

回答我自己的问题。这是一种方法。

widgets = {
            'description': forms.Select(choices=Item.objects.all().values_list('id', 'description'), attrs={})
        }