我有三个桌子,一个是mandal,村庄和财产,在属性表中我将mandal和村庄与外键联系起来,现在当我选择mandal时,只有那个特定的村庄结果必须以django的财产形式显示管理员。我还将mandal与村庄联系起来。
#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 / property时,如何让村庄列表到admin中属性的特定mandal
答案 0 :(得分:0)
我不太确定你想要如何显示你的数据,但是你看过inlineModelAdmin。
管理界面能够在与父模型相同的页面上编辑模型。这些被称为内联。
我使用它来编辑管理页面中的记录,该记录还显示另一个也可以编辑的表中的子记录。在您的情况下,您可以使用它来单击属性进行编辑,并且在属性下面的编辑页面上,它可以显示与其相关的所有Mandals或村庄。如果要链接3个表,可以使用“通过”选项进行链接。查看以下文档:
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin