【发布时间】:2018-06-26 08:32:09
【问题描述】:
我正在使用 Django 创建一个 Web 应用程序。
在我的 models.py 中,我有一个类 BaseProduct 和一个类 DetailProduct,它扩展了 BaseProduct。
在我的 admin.py 中,我有 BaseProductAdmin 类和 DetailProductAdmin 类,它们扩展了 BaseProductAdmin。
我有另一个名为 System 的类,它与 BaseProduct 有多对多的关系。
在系统管理页面中,我可以可视化与该系统相关的 BaseProduct 对象列表。
当我单击产品时,应用程序会将我重定向到 BaseProduct 管理页面。
当列表中的产品是 DetailProduct 对象时,我希望在 DetailProduct 管理页面上被重定向。
你知道怎么做吗?
在 models.py 中:
class BaseProduct(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
_prod_type_id = models.ForeignKey(
ProductTypes, verbose_name="product type", db_column='_prod_type_ID')
systems = models.ManyToManyField(
'database.System', through='database.SystemProduct')
def connected_to_system(self):
return self.systems.exists()
class Meta:
db_table = u'products'
verbose_name = "Product"
ordering = ['id', ]
class System(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
name = models.CharField(max_length=300)
def has_related_products(self):
""" Returns True if the system is connected with products. """
return self.products_set.exists()
class Meta:
managed = False
db_table = u'systems'
verbose_name = "System"
ordering = ['id', ]
class DetailProduct(BaseProduct):
options_id = models.AutoField(db_column='ID', primary_key=True)
product = models.OneToOneField(BaseProduct, db_column='_product_ID', parent_link=True)
min_height = models.FloatField(help_text="Minimum height in meters.")
max_height = models.FloatField(help_text="Maximum height in meters.")
def __init__(self, *args, **kwargs):
super(DetailProduct, self).__init__(*args, **kwargs)
if not self.pk:
self._prod_type_id = ProductTypes.objects.get(pk=9)
class Meta:
managed = False
db_table = 'detail_product'
verbose_name = "Detail product"
verbose_name_plural = "Detail products"
class SystemProduct(models.Model):
id = models.AutoField(primary_key=True, db_column='ID')
_system_id = models.ForeignKey(System, db_column='_system_ID')
_product_id = models.ForeignKey(BaseProduct, db_column='_Product_ID')
class Meta:
db_table = u'system_product'
unique_together = ('_system_id', '_product_id')
verbose_name = "system/product connection"
在我的 admin.py 页面中:
class SystemProductInlineGeneric(admin.TabularInline):
model = SystemProduct
extra = 0
show_edit_link = True
show_url = True
class SystemProductForm(forms.ModelForm):
class Meta:
model = SystemProduct
fields = '__all__'
def __init__(self, *args, **kwargs):
""" Remove the blank option for the inlines. If the user wants to remove
the inline should use the proper delete button. In this way we can
safely check for orphan entries. """
super(SystemProductForm, self).__init__(*args, **kwargs)
modelchoicefields = [field for field_name, field in self.fields.iteritems() if
isinstance(field, forms.ModelChoiceField)]
for field in modelchoicefields:
field.empty_label = None
class SystemProductInlineForSystem(SystemProductInlineGeneric):
""" Custom inline, used under the System change page. Prevents all product-system
connections to be deleted from a product. """
form = SystemProductForm
raw_id_fields = ("_product_id",)
class SystemAdmin(admin.ModelAdmin):
inlines = [SystemProductInlineForSystem]
actions = None
list_display = ('id', 'name')
fieldsets = [('System information',
{'fields': (('id', 'name',), ),}),
]
list_display_links = ('id', 'configuration',)
readonly_fields = ('id',)
save_as = True
【问题讨论】:
-
请在您的频道管理页面中发布您的模型定义和显示“BaseProducts 列表”的代码。
-
我发布了代码。在上一条消息中我犯了一个错误,如您所见,另一个对象的名称是System而不是Channel。
-
我看不到“BaseProducts 列表”的生成位置...您是否覆盖了 SystemAdmin 的管理模板?
-
不,我没有。我正在使用默认模板
-
呃抱歉,我没有发现
SystemProduct是 many2many“通过”模型。
标签: python django python-2.7 inheritance python-requests