我有这些“典型”模型,它们或多或少类似于我的django应用程序,对于该应用程序,我想使用管理界面,足以满足我的目的(但3个月后,我仍然对Django一点都不擅长)
class genericItem(models.Model):
#a genericItem can be a monitor (with its properties) a laptop (with its properties..) etc..
genericItem_ID = models.AutoField( primary_key=True) # Field name made lowercase.
genericItemName = models.CharField(max_length=30, blank=True, null=True)
category = models.ForeignKey('categories', models.DO_NOTHING, db_column='category_ID', null=True,blank=True)
def __str__(self):
return self.genericItemName
class item(commonFields):
#items are goods that may refer to genericItems, e.g. I buy 4 items and 3 of them are monitors (genericItems)
item_ID = models.AutoField(db_column='item_ID', primary_key=True) # Field name made lowercase.
genericItem = models.ForeignKey('genericItem', models.DO_NOTHING,related_name='genericItem', db_column='genericItem_ID')
location = models.ForeignKey('location', models.DO_NOTHING,db_column='room_ID',blank=True, null=True)
serialnumber = models.CharField(db_column='serialNumber', max_length=20, blank=True, null=True) # Field name made lowercase.
accessory = models.ForeignKey('self', models.DO_NOTHING, db_column='accessory', blank=True, null=True)
def __str__(self):
return self.genericItem.genericItemName
class tblFiles(models.Model):
#one could upload files for a item or genericItem
file_ID = models.AutoField(primary_key=True)
filename = models.FileField(null=False, blank=False, upload_to=("somewhere"))
description = models.CharField(max_length=200, blank=True, null=True)
item = models.ForeignKey('item', models.DO_NOTHING, db_column='item_ID', blank=True, null=True, related_name="itemFile")
genericItem = models.ForeignKey('genericItem', models.DO_NOTHING, db_column='genericItem_ID', blank=True, null=True, related_name="genericItemFile")
def __str__(self):
return os.path.basename(self.filename.name)
class tblFilesInline(admin.TabularInline):
model = tblFiles
extra = 0
fields = ['filename','description']
class itemAdmin(admin.ModelAdmin):
list_display = ['genericItem','fieldOne','location','serialnumber']
fieldsets = [(None,{'fields':['genericItem','fieldOne','location','serialnumber',]})]
inlines = [tblFilesInline ]
class genericItemAdmin(admin.ModelAdmin):
fieldsets = [
(None,{'fields':['genericItemName','category']})]
list_display=('genericItemName','category')
inlines = [tblFilesInline ]
我认为,由于item
和genericItem
是通过外键链接的,因此itemAdmin
中的tabularInline会同时显示来自item
和genericItem
的文件,但是当然不是,我只能看到item
中的文件。
如何查看两者的文件?这将帮助用户避免重复文件,因为当他添加item
并将其链接到genericItem
时,他可以看到照片已经存在,并且可以添加其他文件(到item
)
谢谢!