4

入力された日付のペアが現在モデルに保存されている日付範囲に含まれていないことを確認するために、フォームのクリーンなメソッドを作成しようとしています。

キャッチしたいのですが、クリーンメソッドfromdate >= value <= todateのフィルターがそれを取得していません。between

誰かが啓発を提供できますか?

モデル.py

class Dates(models.Model):
    fromdate = models.DateField()
    todate = models.DateField()

Forms.py

class Form(forms.Form):
    fromdate = forms.DateField()

    todate = forms.DateField()

    class Meta:
        model = Dates

    def clean_fromdate(self):
        form_fromdate = self.cleaned_data.get('fromdate')

        exists = Dates.objects.filter(fromdate__contains=form_fromdate).exists()
        if exists == True:
            raise forms.ValidationError(_("A period is already entered for this date"))

        between = Dates.objects.filter(fromdate__gte=form_fromdate,
            todate__lte=form_fromdate).exists()
        if between == True:
            raise forms.ValidationError(_("Period already between this date 1"))

        return form_fromdate


    def clean_todate(self):
        form_todate = self.cleaned_data.get('todate')

        exists = Dates.objects.filter(todate__contains=form_todate).exists()
        if exists == True:
            raise forms.ValidationError(_("A period is already entered for this date"))

        between = PeriodsOfInactivity2012.objects.filter(fromdate__gte=form_todate,
            todate__lte=form_todate).exists()
        if between == True:
            raise forms.ValidationError(_("Period already between this date 2"))

        return form_todate
4

1 に答える 1

10

これはあなたのために働くはずです

from django import forms
from models import Dates

class Form(forms.ModelForm):
    fromdate = forms.DateField()
    todate = forms.DateField()

    class Meta:
        model = Dates

    def clean(self):
        form_fromdate = self.cleaned_data.get('fromdate')
        form_todate = self.cleaned_data.get('todate')
        between = Dates.objects.filter(fromdate__gte=form_fromdate, todate__lte=form_todate).exists()
        if between:
            raise forms.ValidationError("Period already between this dates")
        super(Form, self).clean()

編集

#views.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response

from forms import Form

def index(request, template='index.html'):
    form = Form()
    if request.method == "POST":
        form = Form(request.POST)
        if form.is_valid():
            return HttpResponse(form)

    kwvars = {
        "form": form,
    }
    return render_to_response(template, kwvars, context_instance=RequestContext(request))

#forms.py
from django import forms
from models import Dates

class Form(forms.ModelForm):
    fromdate = forms.DateField()
    todate = forms.DateField()

    class Meta:
        model = Dates

    def clean(self):
        form_fromdate = self.cleaned_data.get('fromdate')
        form_todate = self.cleaned_data.get('todate')
        between = Dates.objects.filter(fromdate__gte=form_fromdate, todate__lte=form_todate).exists()
        if between:
            raise forms.ValidationError("Period already between this date 1")
        super(Form, self).clean()

#index.html
<html>
<head>
</head>
<body>
<form action="" method="POST">
    {% csrf_token %}
    {%  if form.errors %}
        errors: {{ form.errors }}
    {% endif %}
    {{ form }}
    <input type="submit" value="submit">
</form>
</body>
</html>
于 2012-10-23T13:41:40.307 に答える