django-bootstrap-datepicker-plus

https://django-bootstrap-datepicker-plus.readthedocs.io/en/latest/Usage.html#types-of-datepickers

https://django-bootstrap-datepicker-plus.readthedocs.io/en/latest/index.html

https://django-bootstrap-datepicker-plus.readthedocs.io/en/latest/Getting_Started.html

https://github.com/monim67/django-bootstrap-datepicker-plus/blob/5.0.0/dev/mysite/settings.py#L140-L250

keyboard操作:苦手なキーボードのキーの読み方 メモ

パソコンを使用している時に、キーの読み方がパッと出てこなくて、思考が止まってしまうことがあるので、余計なストレスを解消するために作成

苦手なキーの読み方一覧:

※ 実際にはより多くのパターンがありますが、個人的に覚えたいものを抽出しています。

key 読み方(ja) 読み方(en)
, カンマ comma
. ピリオド、ドット period、dot
: コロン colon
; セミコロン semicolon
[ ] 大カッコ(カクカッコ) square bracket
{ } 中カッコ curly brace
^ キャレット caret
` バッククオート back apostrophe
~ チルダ tilde

参考:

https://www.benricho.org/symbol/kigou_11.html

https://notepm.jp/markdown-table-tool

Error:Djangoでマイグレート(migrate)ができない時の対処法「最終手段?」(docker-compose /PostgreSQL)

Error Message:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration account.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.


Djangoで開発中、カスタムユーザーを作成したのちに、migrationファイルの作成、migrateを実行しようとした際にハマりました。

色々と検索をかけて、プロジェクトフォルダのsettings.pyファイルのInstallAppsコメントアウトをしたり、

migrationファイルのロールバックをかけたりしようとしましたが、そもそもmigrateができないから機能しない。。そんな事態。

原因としてはdjango-allauthをカスタムユーザー作成よりも前段でインストールしてmigrateしていたことにありそうだと考えていました。

検索記事の中に、「データベースをドロップする」と言う手段をとっているものがあり、一か八か、こちらを実行した際の顛末になります。


開発環境:
* Mac
* Django
* docker-compose
* PostgreSQL

% docker-compose run --rm web python3 manage.py makemigrations accounts
[+] Running 1/0
 ⠿ Container drinking-party-ms-db-1  Running                 0.0s
Traceback (most recent call last):
:
略)
:
raise InconsistentMigrationHistory(
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration account.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.

既存のマイグレート情報が邪魔している模様 raise InconsistentMigrationHistory( django.db.migrations.exceptions.InconsistentMigrationHistory: Migration account.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.

  • 過去のmigration履歴を以下のコマンドで確認
% docker-compose run --rm web python3 manage.py showmigrations 
[+] Running 1/0
 ⠿ Container drinking-party-ms-db-1  Run...                                    0.0s
account
 [X] 0001_initial
 [X] 0002_email_max_length
accounts
 [ ] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
socialaccount
 [X] 0001_initial
 [X] 0002_token_max_lengths
 [X] 0003_extra_data_default_dict

accountっていうのは、Git管理対象にはないアプリだ。おそらく、DB内にある

account
 [X] 0001_initial
 [X] 0002_email_max_length
accounts
 [ ] 0001_initial
  • dockerのDBコンテナへに侵入
docker-compose exec db bash
psql -U postgres
root@d0db338505fe:/# psql -U postgres
psql (15.1 (Debian 15.1-1.pgdg110+1))
Type "help" for help.

postgres=# DROP SCHEMA public CASCADE;
NOTICE:  drop cascades to 17 other objects
DETAIL:  drop cascades to table django_migrations
drop cascades to table django_content_type
drop cascades to table auth_permission
drop cascades to table auth_group
drop cascades to table auth_group_permissions
drop cascades to table auth_user
drop cascades to table auth_user_groups
drop cascades to table auth_user_user_permissions
drop cascades to table account_emailaddress
drop cascades to table account_emailconfirmation
drop cascades to table django_admin_log
drop cascades to table django_session
drop cascades to table django_site
drop cascades to table socialaccount_socialaccount
drop cascades to table socialaccount_socialapp
drop cascades to table socialaccount_socialapp_sites
drop cascades to table socialaccount_socialtoken
DROP SCHEMA
postgres=# 
postgres=# CREATE SCHEMA public;
CREATE SCHEMA

綺麗になった

% docker-compose run --rm web python3 manage.py showmigrations              
[+] Running 1/0
 ⠿ Container drinking-party-ms-db-1  Running                    0.0s
account
 [ ] 0001_initial
 [ ] 0002_email_max_length
accounts
 (no migrations)
admin
 [ ] 0001_initial
 [ ] 0002_logentry_remove_auto_add
 [ ] 0003_logentry_add_action_flag_choices
auth
 [ ] 0001_initial
 [ ] 0002_alter_permission_name_max_length
 [ ] 0003_alter_user_email_max_length
 [ ] 0004_alter_user_username_opts
 [ ] 0005_alter_user_last_login_null
 [ ] 0006_require_contenttypes_0002
 [ ] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length
 [ ] 0009_alter_user_last_name_max_length
 [ ] 0010_alter_group_name_max_length
 [ ] 0011_update_proxy_permissions
 [ ] 0012_alter_user_first_name_max_length
contenttypes
 [ ] 0001_initial
 [ ] 0002_remove_content_type_name
party
 (no migrations)
sessions
 [ ] 0001_initial
sites
 [ ] 0001_initial
 [ ] 0002_alter_domain_unique
socialaccount
 [ ] 0001_initial
 [ ] 0002_token_max_lengths
 [ ] 0003_extra_data_default_dict

この後、マイグレーションとマイグレートを通常どおり実行することで、無事、データベースへの反映がなされました!

参考

Djangoのマイグレーションとデータベースのリセット方法 | Hodalog postgresql - Django Unit Tests - Unable to create the django_migrations table - Stack Overflow

画像解説:タグの補完機能 "Emmet" が使えない場合の対処方法(Visual Studio Code)

Visual Studio CodeVS Code)を使用してコーディングをされている方向けの記事となります。

ある時、VS CodeでHTMLコードを記述している時に

あれ、何か使いにくいぞ。

機能名はわからないけど、タグの補完機能?が効かない。。。

と言う事態に陥りました。こう言う些細な部分でも結構、メンタルの影響があるものです。

原因を調べると、Visual Studio CodeEmmet と言う補完機能が働いていないことが分かりました。

現状:

画像のように、「 . 」や 「 ! 」+ [.tabキー ] で HTML要素を補完作成されていたはずが、効かない。。

解決方法:

PC : Mac

  • VS Code を開いた状態で、PC画面左上 [Code] > [Preference] > [Settings]
  • VS Code 上部に出現する検索窓へ [.emmet ] と入力

  • [.emmet ] の設定画面が出現するので、下の方へスクロールすると、
    [ Emmet : Trigger Expantion On Tab ] を発見!

  • あとは、そこへチェックを付けてあげて完了です!



以上、VS Codeで補完機能を使えるようにする方法のご紹介でした。
タグ補完機能の詳細な使い方はこちらを参考にどんどん効率化を進めていきたいところです!

参考:

Emmet in Visual Studio Code

随時更新:個人開発中によく使うサイト集

個人開発中によく使うサイト集

Django3.2ドキュメント:
https://docs.djangoproject.com/en/3.2/

Djangoで作られたサイト集:
https://djangosites.org/stats/

Python3.11ドキュメント:
https://docs.python.org/ja/3/

Quickstart: Compose and Django
https://docs.docker.com.zh.xy2401.com/v17.09/compose/django/

MDN_Djangoウェブフレームワーク (Python):
https://developer.mozilla.org/ja/docs/Learn/Server-side/Django

Bootstrap5ドキュメント:
https://getbootstrap.jp/docs/5.0/getting-started/introduction/

Django-bootstrap5 ドキュメント:
https://django-bootstrap-v5.readthedocs.io/en/latest/

とほほのBootstrap 5入門:
https://www.tohoho-web.com/bootstrap5/about.html#diff

Python:大量レコードのCSVファイルで特定のカラムの重複を集計、テキストファイルへ出力する

大量レコードのCSVファイルから特定の重複カラムについて確認しないといけない場面があり、Pythonを活用してトライした際の備忘録です。 今回は、結果の出力をテキストファイルへしましたので、そちらを知りたい方は後半をご覧ください。

github.com

  • 環境
venv
Python 3.9.6
folder
└── file.csv

方法

  • 今回使用するPythonモジュールをインストール
import pandas as pd
import codecs


  • 分析したいcsvファイルを読み込む
data = pd.read_csv('file.csv')


  • Error 発生、今回のファイルだとデータ型に問題がある
<stdin>:1: DtypeWarning: Columns (3,4,25) have mixed types. Specify dtype option on import or set low_memory=False.


  • エラーコードを元に read_csvlow_memory 引数を False に設定する
data = pd.read_csv('file.csv', low_memory=False)


  • DataFrameで変数dataを扱えるようにする
df = pd.DataFrame(data)


  • 今回はgenderカラムの重複データが各何個あるか集計
duplicates = df.pivot_table(index =['gender'], aggfunc = 'size')


  • 集計の出力を新たなnew_file.txtファイルへ出力(同名ファイルが既存の場合、上書き)
print(duplicates, file=codecs.open('new_file.txt', 'w', 'utf-8'))


↓作成された集計が出力されたテキストファイル

補足

レコード数が不明かつ膨大だったため、あるカラムの集計を上記でする際に、以下、set_option()を活用 データフレームのレコードが途中で省略されることを阻止できます。

pd.set_option("display.max_rows",None)

参考

pandas.pydata.org

low_memoryブール値、デフォルトは True ファイルをチャンクで内部的に処理するため、解析中のメモリ使用量が少なくなりますが、型推論が混在する可能性があります。タイプが混在しないようにするには、False を設定するか、dtypeパラメータでタイプを指定します。ファイル全体が単一の DataFrame に読み込まれることに注意してください。