Django:表单查询数据库

时间:2015-02-25 12:05:41

标签: django django-forms django-templates django-views django-queryset

我希望用户能够执行以下查询:

在没有博士的情况下取得所有人,并在两个日期内与合同签订全职合同。

在Django中翻译:

Contract.objects.filter(
    person__is_doctor = False,
    type_contract = 'full',
    starting_date__gte = start_date,
    ending_date__lte = end_date
    )

如何制作表单/视图/模板以允许用户输入 start_date end_date 并显示结果?

模型

class Person(models.Model):
        name    = models.CharField(max_length=32)
        surname = models.CharField(max_length=32)
        address = models.CharField(max_length=32)
        is_doctor  = models.NullBooleanField(blank=True, verbose_name=_(u"Phd?")

TYPE_CONTRACT = (
    ('PT', 'Partial time'),
    ('FC', 'Full contract')
    )           

class Contract(models.Model):
        person        = models.ForeignKey(Person) #person hired
        type_contract = models.CharField(max_length = 9, blank = True, choices = TYPE_CONTRACT)
        starting_date = models.DateField(blank = True, null = True)
        ending_date   = models.DateField(blank = True, null = True)

2 个答案:

答案 0 :(得分:2)

首先,我会重构Contract模型以更好地封装选择,以及为什么合同允许开始和结束日期的空值?从业务逻辑的角度来看,这对我来说似乎很奇怪。

class Contract(models.Model):
    PT = 'Part Time'
    FC = 'Full Contract'
    CONTRACT_CHOICES = (
        ('PT', PT),
        ('FC', FC)
    )
    person        = models.ForeignKey(Person)
    type_contract = models.CharField(max_length=9, choices=CONTRACT_CHOICES,
        default=PT)
    starting_date = models.DateField()
    ending_date   = models.DateField()

# view

from django.shortcuts import render
from .forms import ContactForm

def filter_contracts(request):
    form = ContractForm(request.POST or None)
    contracts = None

    if request.method == 'POST':
        if form.is_valid():
            # encapsulating the contract values means you don't have to
            # hand-code them in the query so your code stays DRY
            contracts = Contract.objects.filter(person__is_doctor=False,
                type_contract=Contract.FC,
                starting_date__gte=form.cleaned_data.get('starting_date'),
                ending_date__lte=form.cleaned_data.get('ending_date'))
            # you might want to specify an order_by here
    return render(request, 'your_template.html', {'form': form,
        'contracts': contracts})

# template

<form action="." enctype="application/x-www-form-urlencoded" method="post">
    <ol>
        {{ form.as_ul }}
    </ol>
    {% csrf_token %}
    <button type="submit">Search</button>

    {% if contracts %}
    <table>
        {% for contract in contracts %}
        <tr>
            <td>{{ contract.person }}</td>
            <td>{{ contract.type_contract }}</td>
            <td>{{ contract.starting_date }}</td>
            <td>{{ contract.ending_date }}</td>
        </tr>
        {% endfor %}
    </table>
    {% endif %}
</form>

答案 1 :(得分:0)

这是主要的想法,另外你必须检查start_date和end_date是否实际上在cleaning_data中,并在使用filter()时将其考虑在内。

<强>形式

class MyForm(forms.Form):
    start_date = forms.DateField(initial=datetime.date.today)
    end_date = forms.DateField(initial=datetime.date.today)

查看

def my_view(request):
    contracts = Contract.objects.filter(person__is_doctor=False, type_contract='full')
    # if this is a POST request take start_date and end_date into account
    if request.method == 'POST':
        form = MyForm(request.POST)
        if form.is_valid():
            contracts = contracts.filter(starting_date__gte=form.cleaned_data.get('start_date'), ending_date__lte=form.cleaned_data.get('end_date'))
    else:
        form = MyForm()
    # do everything else that you need to do before returning a response
    return render_to_response('template.html', locals(), context_instance=RequestContext(request))

<强>模板

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

{% for contract in contracts %}
    {{ contract.person }}
{% endfor %}