我正在使用django-tables2来生成generic.listview。我有3个涉及(简化)GenericDevice的模型,GenericDevice是从抽象类Device继承的,Invoice是外键,InvoiceSupplier是Invoice中的外键。所有类都定义了get_absolute_url。我想做的是在django-table中显示外键InvoiceSupplier,作为指向对象detailview的链接。
#inventory / models.py
从invoices.models导入发票,供应商,制造商
class Device(models.Model): serial_number = models.CharField( max_length=20, unique=True, validators=[digits_chars] ) inventory_number = models.CharField( max_length=20, unique=True, default='Update THIS!', validators=[digits_chars], ) invoice = models.ForeignKey( Invoice, on_delete= models.PROTECT, ) manufacturer = models.ForeignKey( Manufacturer, on_delete= models.PROTECT, ) class Meta: abstract = True class GenericDevice(Device): owner = models.ForeignKey( User, on_delete=models.PROTECT, null= True, blank = True, ) type = models.ForeignKey( DeviceType, related_query_name='generic_devices', default=6, on_delete=models.PROTECT, ) device = models.ForeignKey( NetworkDevice, on_delete=models.PROTECT, null=True, blank=True, )
#invoices / models.py
class contact(models.Model):
name = models.CharField( max_length=120, unique=True, help_text="Contact Name/Company Name", ) address = models.TextField( max_length=255, blank=True, ) phone_number = models.CharField( max_length=13, blank=True, ) emailaddress = models.EmailField( blank = False, unique=True, default='johdoe@changeme.com' ) class Meta: abstract = True
类供应商(联系人): TIN = models.CharField( max_length = 50, name ='TIN', ) 说明= models.TextField( null =真, )
类发票(models.Model):
#If plural not corectly configured in admin(override) #class Meta: # verbose_name_plural = 'Invoices' INVOICE_STATUS =( ('P', 'Pending'), ('A', 'Approved'), ('R', 'Rejected'), ) InvoiceNumber = models.CharField( max_length=30, default='Please Enter Serial', help_text='Invoice serial number', verbose_name='Number', unique=True, ) InvoiceDate = models.DateField( default=date(2018,1,1), help_text='Date is in year, month, day format', verbose_name='Date of issue', ) InvoiceDescription = models.TextField( default='Please enter a description for this invoice', help_text='Description should contain ...', verbose_name='Description', ) InvoiceValue = models.DecimalField( max_digits=15, decimal_places=2, default=999, verbose_name='Amount (RON)', ) InvoiceVAT = models.IntegerField( help_text='VAT value(percent)', default = 50, verbose_name='VAT (%)' ) InvoiceSupplier = models.ForeignKey( Supplier, default=DEFAULT_SUPPLIER_ID, on_delete=models.PROTECT, verbose_name='Supplier', )
和#tables.py for GenericDevice
GenericDeviceTable(tables.Table)类:
serial_number = tables.LinkColumn() invoice = tables.RelatedLinkColumn() supplier = tables.RelatedLinkColumn(accessor='invoice.Supplier') class Meta: model = GenericDevice
该表将外键发票呈现为对象的DetailView的链接,但不呈现外键发票的外键提供者。 我在做什么错了?
我尝试使用Supplier = tables.RelatedLinkColumn(accessor ='Invoice.InvoiceSupplier'),效果相同:字段为空。