[Django]ModelFormのバリデーション

– バリデーションはsaveの時に実行されるので、モデルの更新時にはモデル側でバリデーションをかける

/hello/forms.py

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']

/hello/models.py

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(default=0)
	birthday = models.DateField()

– save以外に自分でチェックをかけることも可能
/hello/views.py

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': FriendForm(),
	}
	if(request.method == 'POST'):
		obj = Friend()
		form = FriendForm(request.POST, instance=obj)
		params['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'check' %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr><th></th><td>
		<input type="submit" value="click" class="btn btn-primary mt-2">
		</td></tr>
		</table>
	</form>
</body>

– forms.FormとModelFormのバリデーションは異なる

### モデルにバリデーションの組み込み
/hello/models.py

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(150)])
	birthday = models.DateField()

### モデルで使えるバリデータ
– MinValueValidator/MaxValueValidator, MinLengthValidator/MaxLengthValidator

from django.db import models
from django.core.validators import MinLengthValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[MinLengthValidator(10)])
	mail = models.EmailField(max_length=200, validators=[MinLengthValidator(10)])
	gender = models.BooleanField()
	age = models.IntegerField()
	birthday = models.DateField()

– EmailValidator/URLValidator
– ProhibitNullCharactersValidator
– RegexValidator

from django.core.validators import RegexValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[RegexValidator(r'^[a-z]*$')])

バリデーション定義によってmodelに組み込んでいくイメージです。

[Django]バリデーションの種類

### CharFieldのバリデーション
– required
– min_length, max_length
– empty_value

class CheckForm(forms.Form):
	empty = forms.CharField(label='Empty', empty_value=True, widget=forms.TextInput(attrs={'class':'form-control'}))
	min = forms.CharField(label='Min', min_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))	
	max = forms.CharField(label='Max', max_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))

### IntegerField/FloatFieldのバリデーション
– required
– min_value, max_value

class CheckForm(forms.Form):
	required = forms.IntegerField(label='Required', widget=forms.NumberInput(attrs={'class':'form-control'}))
	min = forms.IntegerField(label='Min', min_value=100, widget=forms.NumberInput(attrs={'class':'form-control'}))	
	max = forms.IntegerField(label='Max', max_value=1000, widget=forms.NumberInput(attrs={'class':'form-control'}))

### 日時関連のバリデーション
– DateField, TimeField, DateTimeFieldでは日時の各値を表す記号を組み合わせて作成する
– %y, %m, %d, %H, %M, %S

class CheckForm(forms.Form):
	date = forms.DateField(label='Date', input_formats=['%d'], widget=forms.DateInput(attrs={'class':'form-control'}))
	time = forms.TimeField(label='Time', widget=forms.TimeInput(attrs={'class':'form-control'}))
	datetime = forms.DateTimeField(label='DateTime', widget=forms.DateTimeInput(attrs={'class':'form-control'}))

### バリデーションを追加
– Formクラスにメソッドを追加する
– raise ValidationError(“Error message”)
/hello/forms.py

from django import forms

class CheckForm(forms.Form):
	str = forms.CharField(label='String', widget=forms.TextInput(attrs={'class':'form-control'}))

	def clean(self):
		cleaned_data = super().clean()
		str = cleaned_data['str']
		if(str.lower().startswith('no')):
			raise forms.ValidationError('Your input "No!"')

[Django]バリデーション基礎

if(<

>.is_valid()):
# エラー時の処理
else:
# 正常時の処理

### バリデーションを使ってみる
/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'check' %}" method="post">
		{% csrf_token %}
		{{ form.as_table }}
		<input type="submit" value="click" class="btn btn-primary mt-2">
	</form>
</body>

/hello/urls.py

	path('check', views.check, name='check')

/hello/forms.py

class CheckForm(forms.Form):
	str = forms.CharField(label='Name', widget=forms.TextInput(attrs={'class':'form-control'}))	

/hello/views.py

from .forms import CheckForm

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': CheckForm(),
	}
	if(request.method == 'POST'):
		form = CheckForm(request.POST)
		param['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

スペルミスでmethod=”post”がmetho=”post”になってた。道理で動かない訳だ。

[Django]SQLを直接実行

Managerクラスに用意されている「raw」メソッドを使う

/hello/views.py

def find(request):
	if(request.method == 'POST'):
		msg = request.POST['find']
		form = FindForm(request.POST)
		sql = 'select * from hello_friend'
		if(msg != ''):
			sql += ' where ' + msg
		data = Friend.objects.raw(sql)
		msg = sql
	else:
		msg = 'search words...'
		form = FindForm()
		data =Friend.objects.all()
	params = {
		'title': 'Hello',
		'message': msg,
		'form': form,
		'data': data,
	}
	return render(request, 'hello/find.html', params)

テーブル名はhello_friend
マイグレーションの際に「アプリケーション名_モデル名」で作成される

まあ、SQLの方が慣れているといえば慣れてます。

[Django]レコードの並び替え

Managerクラスの「order_by」メソッドで行う

/hello/views.py

def index(request):
	data = Friend.objects.all().order_by('age')
	params = {
		'title': 'Hello',
		'message': '',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

/hello/templates/hello/index.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th>age</th>
			<th>mail</th>
			<th>birth</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td>{{item.age}}</td>
			<td>{{item.mail}}</td>
			<td>{{item.birthday}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

逆の場合はorder_by().reverse()とする

data = Friend.objects.all().order_by('age').reverse()

### 指定した範囲のレコードを取り出す
/hello/views.py

def find(request):
	if(request.method == 'POST'):
		msg = 'Search Result:'
		form = FindForm(request.POST)
		find = request.POST['find']
		list = find.split()
		data = Friend.objects.all()[int(list[0]):int(list[1])]
	else:
		msg = 'search words...'
		form = FindForm()
		data =Friend.objects.all()
	params = {
		'title': 'Hello',
		'message': msg,
		'form': form,
		'data': data,
	}
	return render(request, 'hello/find.html', params)

/hello/templates/hello/find.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'find' %}" method="post">
		{% csrf_token %}
		{{ form.as_p }}
		<tr>
			<th></th><td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</form>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th>age</th>
			<th>mail</th>
			<th>birthday</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td>{{item.age}}</td>
			<td>{{item.mail}}</td>
			<td>{{item.birthday}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

### レコードの集計
aggregateメソッドで集計を行わせる
– count: レコード数
– sum: 合計
– avg: 平均
– min: 最小値
– max: 最大値

/hello/views.py

from django.db.models import Count, Sum, Avg, Min, Max

def index(request):
	data = Friend.objects.all()
	re1 = Friend.objects.aggregate(Count('age'))
	re2 = Friend.objects.aggregate(Sum('age'))
	re3 = Friend.objects.aggregate(Avg('age'))
	re4 = Friend.objects.aggregate(Min('age'))
	re5 = Friend.objects.aggregate(Max('age'))
	msg = 'count:' + str(re1['age__count']) + '<br>Sum:' + str(re2['age__sum']) + '<br>Average:' + str(re3['age__avg']) + '<br>Min:' + str(re4['age__min']) + '<br>Max:' + str(re5['age__max'])
	params = {
		'title': 'Hello',
		'message': '',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

avgの小数点以下がおかしなことになってますが、filterを使えば大抵の事は出来るとのこと。

[Django]filterによる検索

モデルにはobjects属性があり、その中にManagerというクラスのインスタンスが入っている
このManagerにfilter機能がある

/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
	path('delete/<int:num>', views.delete, name='delete'),
	path('list', FriendList.as_view()),
	path('detail/<int:pk>', FriendDetail.as_view()),
	path('find', view.find, name='find'), # 追加
]

/hello/forms.py

class FindForm(forms.Form):
	find = forms.CharField(label='Find', required=False, widget=forms.TextInput(attrs={'class':'form-control'}))

/hello/templates/hello/find.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'find' %}" method="post">
		{% csrf_token %}
		{{ form.as_p }}
		<tr>
			<th></th><td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</form>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th>mail</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}({{item.age}})</td>
			<td>{{item.mail}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

/hello/views.py

def find(request):
	if(request.method == 'POST'):
		form = FindForm(request.POST)
		find = request.POST['find']
		data = Friend.objects.filter(name=find)
		msg = 'Result: ' + str(data.count())
	else:
		msg = 'search words...'
		form = FindForm()
		data =Friend.objects.all()
	params = {
		'title': 'Hello',
		'message': msg,
		'form': form,
		'data': data,
	}
	return render(request, 'hello/find.html', params)

Friend.objects.filter(name=find)で指定している。

### 曖昧検索
– 値を含む検索: __contains=value
– 値で始まるもの: __startswith=value
– 値で終わるもの: __endswith=value

if(request.method == 'POST'):
		form = FindForm(request.POST)
		find = request.POST['find']
		data = Friend.objects.filter(name__contains=find)
		msg = 'Result: ' + str(data.count())

– 大文字小文字を区別しない: __iexact=value
– 曖昧検索: __icontains=value, __istartswith=value, __iendswith=value

### 数値の比較
– 等しい: =value
– 大きい: __gt=value
– 以上: __gte=value
– 小さい: __lt=value
– 以下: __lte=value

if(request.method == 'POST'):
		form = FindForm(request.POST)
		find = request.POST['find']
		data = Friend.objects.filter(age__lte=int(find))
		msg = 'Result: ' + str(data.count())

●●以上●●以下
split()は改行やスペースで分割する

	if(request.method == 'POST'):
		form = FindForm(request.POST)
		find = request.POST['find']
		val = find.split()
		data = Friend.objects.filter(age__gte=val[0], age__lte=val[1])
		msg = 'Result: ' + str(data.count())

### AとBどちらも検索
変数 = <<モデル>>.object.filter(Q(条件) | Q(条件))

from django.db.models import Q
// 
data = Friend.objects.filter(Q(name__contains=find)|Q(mail__contains=find))

### リストを使って検索

	if(request.method == 'POST'):
		form = FindForm(request.POST)
		find = request.POST['find']
		list = find.split()
		data = Friend.objects.filter(name__in=list)
		msg = 'Result: ' + str(data.count())

検索条件はアプリケーションの要件や機能によって変わってきますね。

[Django]ジェネリックビュー

ジェネリックビューは指定したモデルから全レコードを取り出したり、特定のIDのものだけ取り出す基本的機能を持ったビュー
ListViewは全レコードを取り出すためのジェネリックビュー
DetailViewは特定のレコードを取り出すためのジェネリックビュー

### ジェネリックビューで表示
/hello/views.py

from django.views.generic import ListView
from django.views.generic import DetailView

class FriendList(ListView):
	model = Friend

class FriendDetail(DetailView):
	model = Friend

/hello/urls.py

from .views import FriendList
from .views import FriendDetail

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
	path('delete/<int:num>', views.delete, name='delete'),
	path('list', FriendList.as_view()),
	path('detail/<int:pk>', FriendDetail.as_view()),
]

/hello/templates/hello/friend_list.html

<body class="container">
	<h1 class="display-4 text-primary">Friends List</h1>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th></th>
		</tr>
	{% for item in object_list %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td><a href="/hello/detail/{{item.id}}">detail</a></td>
		</tr>
	{% endfor %}
	</table>
</body>

/hello/templates/hello/friend_detail.html

<body class="container">
	<h1 class="display-4 text-primary">Friends Detail</h1>
	<table class="table">
		<tr>
			<th>id</th>
			<th>{{object.id}}</th>
		</tr>
		<tr>
			<th>name</th>
			<th>{{object.name}}</th>
		</tr>
		<tr>
			<th>mail</th>
			<th>{{object.mail}}</th>
		</tr>
		<tr>
			<th>gender</th>
			<th>{{object.gender}}</th>
		</tr>
		<tr>
			<th>age</th>
			<th>{{object.age}}</th>
		</tr>
	</table>
</body>

なるほど

[Django]CRUDのDelete

レコードのモデルインスタンスを取得してdeleteメソッドを実行する

urlspaternsの追記
/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
	path('delete/<int:num>', views.delete, name='delete'),
]

index.htmlの修正
/hello/templates/hello/index.html

{% for item in data %}
		<tr>
			<td>{{item}}</td>
			<td><a href="{% url 'edit' item.id %}">Edit</a></td>
			<td><a href="{% url 'delete' item.id %}">Delete</a></td>
		</tr>
	{% endfor %}

delete.htmlの作成
/hello/templates/hello/delete.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>※以下のレコードを削除します。</p>
	<table class="table">
		<tr>
			<th>ID</th>
			<td>{{obj.id}}</td>
		</tr>
		<tr>
			<th>Name</th>
			<td>{{obj.name}}</td>
		</tr>
		<tr>
			<th>Gender</th>
			<td>
				{% if obj.gender == False %} male {% endif %}
				{% if obj.gender == True %} female {% endif %}
			</td>
		</tr>
		<tr>
			<th>Email</th>
			<td>{{obj.mail}}</td>
		</tr>
		<tr>
			<th>Age</th>
			<td>{{obj.age}}</td>
		</tr>
		<tr>
			<th>Birth</th>
			<td>{{obj.birthday}}</td>
		</tr>
		<form action="{% url 'delete' id %}" method="post">
		{% csrf_token %}
		<tr><th></th><td>
			<input type="submit" value="click" class="btn btn-primary">
		</td></tr>
	</form>
	</table>
</body>

delete関数を作る
/hello/views.py

def delete(request, num):
	friend = Friend.objects.get(id=num)
	if(request.method == 'POST'):
		friend.delete()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'id': num,
		'obj': friend,
	} 
	return render(request, 'hello/delete.html', params)

CRUDの基本は抑えました。

[Django]CRUDのUpdate

/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
]

/hello/templates/hello/create.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<table class="table">
		<tr>
			<th>data</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item}}</td>
			<td><a href="{% url 'edit' item.id %}">Edit</a></td>
		</tr>
	{% endfor %}
	</table>
</body>

– url ‘edit’ item.idで/edit/1というようにアクセスできるようになる

edit.htmlを作る
/hello/templates/hello/edit.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'edit' id %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr>
			<td></td>
			<td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</table>
	</form>
</body>

edit関数を作る
/hello/views.py

# edit
def edit(request, num):
	obj = Friend.objects.get(id=num)
	if(request.method == 'POST'):
		friend = FriendForm(request.POST, instance=obj)
		friend.save()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'id': num,
		'form': FriendForm(instance=obj),
	}
	return render(request, 'hello/edit.html', params)

引っ張る時は、’form’: FriendForm(instance=obj),として、updateの時はFriendForm(request.POST, instance=obj)とするのか。なお、createの時はobj = Friend()

[Django]ModelFormを使う

ModelFormを使うことでよりスムーズにレコードの保存を行うことができる

/hello/forms.py

from django import forms
from.models import Friend

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']

/hello/views.py

def create(request):
	if(request.method == 'POST'):
		obj = Friend()
		friend = FriendForm(request.POST, instance=obj)
		friend.save()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'form': HelloForm(),
	}
	return render(request, 'hello/create.html', params)

– obj = Friend()でインスタンスを作成し、FriendForm(request.POST, instance=obj)インスタンスを作成する

/hello/templates/hello/create.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'create' %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr>
			<td></td>
			<td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</table>
	</form>
</body>

先が長いな。