0
我有三個表,其中一個是曼德爾,村莊和財產,在屬性表中我已經鏈接曼陀羅和村莊與外鍵,現在當我選擇mandal,只有那個特定的村莊結果應該在django admin中以財產形式顯示。我也把曼達與村莊聯繫在一起。如何獲得外鍵的信息,當選擇到另一個表時,當選擇特定的鍵
#models.py
from django.db import models
from django.utils import timezone
class Mandal(models.Model):
id = models.AutoField(
primary_key=True,
db_column="Mandal_id",
verbose_name="Mandal Id",
)
name = models.CharField(
max_length=200,
db_column="Mandal_Name",
verbose_name="Mandal Name",
null=False,
blank=False,
help_text="Enter Mandal names only",
default=None,
)
class Meta:
db_table = "Mandal"
verbose_name_plural = "Mandal"
def __str__(self):
return self.name
class Village(models.Model):
id = models.AutoField(
primary_key=True,
db_column="Village_id",
verbose_name="Village Id",
)
name = models.CharField(
max_length=200,
db_column="Village_Name",
verbose_name="Village Name",
null=False,
blank=False,
help_text="Enter village names only",
default=None,
)
mandal_id = models.ForeignKey(
Mandal,
db_column="Mandal_id",
verbose_name="Mandal Name",
)
class Meta:
db_table = "Village"
verbose_name_plural="Village"
def __str__(self):
return self.name
class Properties(models.Model):
id = models.BigAutoField(
primary_key=True,
db_column="Property_id",
verbose_name="Property Id"
)
created_on = models.DateField(
auto_now=False,
auto_now_add=False,
default=timezone.now(),
)
area = models.BigIntegerField(
default=0,
db_column="Property_Area",
verbose_name="Property Area",
help_text="Please enter area in square feet",
validators=[],
)
mandal_location = models.ForeignKey(
Mandal,
db_column="Mandal_id",
verbose_name="Mandal Name",
default=None,
)
village_location = models.ForeignKey(
Village,
db_column="Village_id",
verbose_name="Village Name",
default=None,
)
description = models.TextField(
default=None,
db_column="Property_description",
verbose_name="Property Description",
)
features = models.CharField(
default=None,
db_column="Property_Features",
verbose_name="Property Features",
null=True,
blank=True,
help_text="Add Property Fetures",
max_length=1000,
)
additional_features = models.CharField(
default=None,
db_column="Property_Additional_Features",
verbose_name="Additional Features",
null=True,
blank=True,
help_text="Add Property Fetures",
max_length=1000
)
class Meta:
db_table = "Properties"
verbose_name_plural = "Property"
def __str__(self):
return "Pr-{i}".format(i=self.id)
#admin.py
from django.contrib import admin
from whiteindia.models import Mandal,Village,Properties
class MandalAdmin(admin.ModelAdmin):
list_display = ['id','name']
class Meta:
model = Mandal
class VillageAdmin(admin.ModelAdmin):
list_display = ['id','name','mandal_id']
list_filter = ['mandal_id']
class Meta:
model = Village
class PropertiesAdmin(admin.ModelAdmin):
list_display = ['id', 'area','village_location','mandal_location']
class Meta:
model = Properties
admin.site.register(Mandal,MandalAdmin)
admin.site.register(Village,VillageAdmin)
admin.site.register(Properties,PropertiesAdmin)
那麼如何讓村莊名單到一個特定的曼達爾在物業管理中,當我們在瀏覽器窗口中輸入admin /屬性
在模型 - 財產,如果我選擇曼達爾,我應該得到與特定曼達爾相關的村莊的名單。 –
您的房產列表通過外鍵顯示了一個曼達位置。如果你想點擊這個鍵然後顯示一個單獨的村莊列表,你需要捕獲記錄的主鍵並將它傳遞給另一個視圖。這可能涉及必須覆蓋或部分管理類的子類。您應該查看有關自定義管理部分的文檔以執行此操作。作爲另一種選擇,您可以使用內聯使用「直通」選項來顯示財產的故障列表,並可能顯示曼德爾的村莊列表。取決於你希望你的app看起來如何。 – cander