今回はpythonのフレームワークdjangoのModelについて、紹介します。
Modelの書き方
from django.db import models
class BookInfo(models.Model):
GENDER_CHOICES=(
(0, ‘male’),
(1, ‘female’)
)
name=models.CharField(max_length=10,unique=True,verbose_name=’名前’)
gender=models.SmallIntegerField(choices=GENDER_CHOICES, default=0)
pub_date=models.DateField(null=True)
readcount=models.IntegerField(default=0)
commentcount=models.IntegerField(default=0)
is_delete=models.BooleanField(default=False)
#book=models.ForeignKey(Book, on_delete=models.CASCADE)
class Meta:
# テーブル名を指定する
db_table=’bookinfo’
フィールドの型一覧
フィールドの型 |
AutoField |
BigAutoField |
BigIntegerField |
BinaryField |
BooleanField |
CharField |
CommaSeparatedIntegerField |
DateField |
DateTimeField |
DecimalField |
DurationField |
EmailField |
FileField |
FileField と FieldFile |
FilePathField |
FloatField |
ImageField |
IntegerField |
GenericIPAddressField |
NullBooleanField |
PositiveIntegerField |
PositiveSmallIntegerField |
SlugField |
SmallIntegerField |
TextField |
TimeField |
URLField |
UUIDField |
データ登録
方法1
python manage.py shell
from book.models import BookInfo
book=BookInfo(
name=’test’,
pub_date=’2021-08-24′
)
book.save()
from book.models import BookInfo
book=BookInfo(
name=’test’,
pub_date=’2021-08-24′
)
book.save()
方法2
BookInfo.objects.create(
name=’test2′,
pub_date=’2021-08-25′
)
name=’test2′,
pub_date=’2021-08-25′
)
データの検索
get()
# 条件あり
try:
book=BookInfo.objects.get(id=100)
except BookInfo.DoesNotExist:
pass
try:
book=BookInfo.objects.get(id=100)
except BookInfo.DoesNotExist:
pass
all()
#全件取得
book=BookInfo.objects.all()
book=BookInfo.objects.all()
count()
# 件数取得
book=BookInfo.objects.count()
book=BookInfo.objects.count()
filter()
BookInfo.objects.filter() # 結果複数
BookInfo.objects.get(id__exact=1) または BookInfo.objects.get(id=1)
BookInfo.objects.filter(name___contains=’漢字’) # 存在
BookInfo.objects.filter(name__endwith=’時’)
BookInfo.objects.filter(name__isnull=True)
BookInfo.objects.filter(id__in=[1,3,5])
BookInfo.objects.filter(id__gt=3) #gte
BookInfo.objects.filter(id__lt=3)
BookInfo.objects.get(id__exact=1) または BookInfo.objects.get(id=1)
BookInfo.objects.filter(name___contains=’漢字’) # 存在
BookInfo.objects.filter(name__endwith=’時’)
BookInfo.objects.filter(name__isnull=True)
BookInfo.objects.filter(id__in=[1,3,5])
BookInfo.objects.filter(id__gt=3) #gte
BookInfo.objects.filter(id__lt=3)
exclude()
BookInfo.objects.exclude() # 条件以外のデータ
Fオブジェクト
from django.db.models import F
BookInfo.objects.filter(readcount__gte=F(‘commentcount‘)*2)
BookInfo.objects.filter(readcount__gte=F(‘commentcount‘)*2)
Qオブジェクト
BookInfo.objects.filter(id__gt=2).filter(readcount__gt=20) # and
BookInfo.objects.filter(id__gt=2,readcount__gt=20) # and
from django.db.models import Q
BookInfo.objects.filter(Q(id__gt=2)|Q(readcount__gt=20)) # OR
BookInfo.objects.filter(~Q(id__gt=2)) # NOT
BookInfo.objects.filter(id__gt=2,readcount__gt=20) # and
from django.db.models import Q
BookInfo.objects.filter(Q(id__gt=2)|Q(readcount__gt=20)) # OR
BookInfo.objects.filter(~Q(id__gt=2)) # NOT
sum,max,min,avg,count
from django.db.models import Sum,Max,Min,Avg,Count
BookInfo.objects.aggregate(Sum(‘readcount’))
BookInfo.objects.aggregate(Sum(‘readcount’))
ソート
BookInfo.objects.all().order_by(‘readcount’) # 昇順
BookInfo.objects.all().order_by(‘-readcount’) # 降順
BookInfo.objects.all().order_by(‘-readcount’) # 降順
結合検索
bookinfo=BookInfo.objects.get(id=2)
bookinfo.peopleinfo_set.all()
bookinfo.peopleinfo_set.all()
people = PeopleInfo.objects.get(id=1)
people.book.name
BookInfo.objects.filter(peopleinfo__name=’aa’)
データの更新
方法1
book=BookInfo.objects.get(id=1)
book.readcount=20
book.save()
book.readcount=20
book.save()
方法2
BookInfo.objects.filter(id=1).update(
readcount=200,
commentcount=300
)
readcount=200,
commentcount=300
)
データの削除
方法1
book=BookInfo.objects.get(id=1)
book.delete()
book.delete()
方法2
BookInfo.objects.filter(id=1).delete()