简体   繁体   中英

Django: form to query database

I want a user to be able to perform the following query:

Fetch all the people, without Phd, with full time contract with a contract within two dates.

that translates in Django:

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

How can I make a form/view/template to allow the user enter both start_date and end_date and show the results?

models

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)

First, I would refactor the Contract model to better encapsulate the choices, and also, why would a contract allow null values for the start and end dates? That seems odd to me from a business logic standpoint.

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>

This is the main idea, additionally you will have to check if start_date and end_date are actually in cleaned_data and take that into account when using filter().

form

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

view

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))

template

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM