【问题标题】:Keep relationships between many-to-many fields django-tables2保持多对多字段django-tables2之间的关系
【发布时间】:2021-10-08 14:02:25
【问题描述】:

我正在使用 Django 和 PostgreSQL 开发网络,我正在使用应用程序 django-tables2 创建 HTML 表。

class SampleTable(ColumnShiftTableBootstrap4):
    class Meta:
        model = Sample
        fields = ("name", "sample_id_sex", "pools", "indexes", "gene_cand_lists",)

我的数据库收集下一代测序 (NGS) 数据。 sample_id_sex 是外键(1:N 关系),而poolsindexesgene_cand_lists 是多对多字段,这三行的组合是唯一的:换句话说,它们是相关的。

我的问题是,在每一行中,默认情况下对值进行排序。例如,样本 X 属于池 CES001,具有索引 B2 和基因列表 list3,但它也属于具有索引 A1 和 list1 的池 CES002。在表格中,它们应如下所示(, 作为多对多列中的分隔符):

sample sex pools indexes gene_lists
12-009 male CES001, CES002 B2, A1 list3, list1

但它们看起来像这样:

sample sex pools indexes gene_lists
12-009 male CES001, CES002 A1, B2 list1, list3

这三个字段之间的关系被打破了。有没有办法纠正这个问题?分隔符可以是换行符而不是逗号?

编辑:我认为这个问题还不够清楚。样本表的型号为:

class Sample(models.Model):
   id_sample = models.AutoField(primary_key=True)
   name = models.CharField(unique=True, max_length=20)
   indexes = models.ManyToManyField(Index, through='SamplePoolIndexCand', blank=True)
   pools = models.ManyToManyField(Index, through='SamplePoolIndexCand', blank=True)
   gene_lists = models.ManyToManyField(Index, through='SamplePoolIndexCand', blank=True)

还有中间表:

class SamplePoolIndexCand(models.Model):
    sample_id = models.ForeignKey(Sample, null=True, blank=True, on_delete=models.CASCADE, db_column='id_sample', 
    verbose_name='Mostra')
    pool_id = models.ForeignKey(Pool, null=True, blank=True, on_delete=models.CASCADE, db_column='id_pool', 
    verbose_name='Pool')    
    index_id = models.ForeignKey(Index, null=True, blank=True, on_delete=models.CASCADE, 
    db_column='id_index', verbose_name='Índex')
    gene_cand_list_id = models.ForeignKey(GeneCandList, null=True, blank=True, on_delete=models.CASCADE, 
    db_column='id_gene_cand_list', verbose_name='Llista de gens candidats')

【问题讨论】:

    标签: django database postgresql many-to-many django-tables2


    【解决方案1】:

    您可以在您的模型中使用多对多字段的特定类中的排序标签

    class gene_lists(models.Model):    
        priority = models.PositiveSmallIntegerField(default=16383)
    
    class indexes(models.Model):
       contents = models.ManyToManyField(gene_lists, blank=True, null=True)
    
        class Meta:
            ordering = ['priority', 'contents']
    

    【讨论】:

    • 对不起,我认为我的问题不完整...我将添加示例和中间表模型以使其更清晰。我想要的是保持多对多字段的关系,每个条目都有一个独特的组合。
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 2015-07-02
    • 2012-03-25
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多