From df6e035729c8249f09ea5c9eaa4408b785c199bb Mon Sep 17 00:00:00 2001 From: Preeti Yuankrathok Date: Sat, 9 Dec 2023 18:21:40 +0700 Subject: [PATCH 1/3] Add __str__ to models --- thailand_geography/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/thailand_geography/models.py b/thailand_geography/models.py index 0442050..76e37c7 100644 --- a/thailand_geography/models.py +++ b/thailand_geography/models.py @@ -9,6 +9,9 @@ class Province(models.Model): class Meta: ordering = ('id',) + def __str__(self): + return str(self.name_th) + class District(models.Model): id = models.PositiveIntegerField(primary_key=True) @@ -19,6 +22,9 @@ class District(models.Model): class Meta: ordering = ('id',) + def __str__(self): + return f'{self.province.name_th} / {self.name_th}' + class Subdistrict(models.Model): id = models.PositiveIntegerField(primary_key=True) @@ -31,3 +37,6 @@ class Meta: def get_province(self): return self.district.province + + def __str__(self): + return f'{self.get_province().name_th} / {self.district.name_th} / {self.name_th}' From ce0ab4af371b1cec2d11031bc3877438f00c4f23 Mon Sep 17 00:00:00 2001 From: Preeti Yuankrathok Date: Sat, 9 Dec 2023 18:21:43 +0700 Subject: [PATCH 2/3] Add admin site --- thailand_geography/admin.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 thailand_geography/admin.py diff --git a/thailand_geography/admin.py b/thailand_geography/admin.py new file mode 100644 index 0000000..82f2ecb --- /dev/null +++ b/thailand_geography/admin.py @@ -0,0 +1,28 @@ +from django.contrib import admin + +from .models import Province, District, Subdistrict + + +class ProvinceAdmin(admin.ModelAdmin): + list_display = ('id', 'name_en', 'name_th') + search_fields = ('name_en', 'name_th') + + +class DistrictAdmin(admin.ModelAdmin): + list_display = ('id', 'province', 'name_en', 'name_th') + search_fields = ('name_en', 'name_th', 'province__name_en', 'province__name_th') + list_select_related = ('province',) + + +class SubdistrictAdmin(admin.ModelAdmin): + list_display = ('id', 'province', 'district', 'name_en', 'name_th') + search_fields = ('name_en', 'name_th', 'province__name_en', 'province__name_th', 'district__name_en', 'district__name_th') + list_select_related = ('district__province',) + + def province(self, obj): + return obj.get_province() + + +admin.site.register(Province, ProvinceAdmin) +admin.site.register(District, DistrictAdmin) +admin.site.register(Subdistrict, SubdistrictAdmin) From caa43e04a51de34c58aee0bdb95eebf19c1de75d Mon Sep 17 00:00:00 2001 From: Preeti Yuankrathok Date: Sat, 9 Dec 2023 18:23:23 +0700 Subject: [PATCH 3/3] Fix lint --- thailand_geography/admin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/thailand_geography/admin.py b/thailand_geography/admin.py index 82f2ecb..b4b1b62 100644 --- a/thailand_geography/admin.py +++ b/thailand_geography/admin.py @@ -16,7 +16,9 @@ class DistrictAdmin(admin.ModelAdmin): class SubdistrictAdmin(admin.ModelAdmin): list_display = ('id', 'province', 'district', 'name_en', 'name_th') - search_fields = ('name_en', 'name_th', 'province__name_en', 'province__name_th', 'district__name_en', 'district__name_th') + search_fields = ( + 'name_en', 'name_th', 'province__name_en', 'province__name_th', 'district__name_en', 'district__name_th', + ) list_select_related = ('district__province',) def province(self, obj):