django:相同的tabularInline用于两个ForeignKeys

时间:2018-11-29 12:28:54

标签: django foreign-keys django-admin inline

我有这些“典型”模型,它们或多或少类似于我的django应用程序,对于该应用程序,我想使用管理界面,足以满足我的目的(但3个月后,我仍然对Django一点都不擅长)

models.py

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)

admin.py

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 ]

我认为,由于itemgenericItem是通过外键链接的,因此itemAdmin中的tabularInline会同时显示来自itemgenericItem的文件,但是当然不是,我只能看到item中的文件。
如何查看两者的文件?这将帮助用户避免重复文件,因为当他添加item并将其链接到genericItem时,他可以看到照片已经存在,并且可以添加其他文件(到item
谢谢!

0 个答案:

没有答案