我正在使用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
答案 0 :(得分:0)
如果我正确理解,您的问题是如何更改InlineAdmin(SystemProductInlineForSystem
)模板,以便“更改链接”重定向到DetailProduct
管理员更改表单(而不是BaseProduct
管理员更改表单),而该产品实际上是DetailProduct
。
我从不必处理此用例,因此无法提供完整的确定性答案,但基本上,您将必须override the inlineadmin template来使用SystemProductInlineForSystem
并更改the part of the code that generates this url。
我无法确切地告诉您您将要进行哪些更改(嗯,如果我有几个小时可以花在这个上,但事实并非如此……),所以您必须进行分析这部分代码并自己找出-除非当然有人在...