Skip to content

Instantly share code, notes, and snippets.

@guyjacks
Last active September 24, 2018 00:40
Show Gist options
  • Save guyjacks/cbedf0a87c3cfa2f8889eb6579489090 to your computer and use it in GitHub Desktop.
Save guyjacks/cbedf0a87c3cfa2f8889eb6579489090 to your computer and use it in GitHub Desktop.
# WHAT I HAVE TRIED
def parse_intersection(self, filters):
# each of these two filters are uuids
first_filter = filters[0]['gene']
second_filter = filters[1]['gene']
# Doesn't work - seems to ignore one of the Qs
# This seems to be the ideal way to do this, but it doesn't work.
#qs = Rule.objects.filter(Q(gene_traits__pk=first_filter) and Q(gene_traits__pk=second_filter))
# Doesn't work - seems to ignore one of the filters. This should be the same as the query below.
#qs = Rule.objects.filter(gene_traits__pk=first_filter)
#qs.filter(gene_traits__pk=second_filter)
# Works
qs = Rule.objects.filter(gene_traits__pk=first_filter).filter(gene_traits__pk=second_filter)
return qs
# MODELS
class GeneTrait(
LabelModel,
BiopsyBatchModel,
CreatedModifiedModel,
UUIDModel,
):
gene = models.ForeignKey(
'Gene',
on_delete=models.PROTECT,
related_name='traits'
)
mutated = models.BooleanField()
gene_mutation_variant = models.ForeignKey(
'GeneMutationVariant',
on_delete=models.PROTECT,
null=True,
blank=True
)
position = models.IntegerField(null=True, blank=True)
class Meta:
db_table = 'datasets_gene_traits'
ordering = ['gene__label', 'label']
class Rule(
LabelModel,
BiopsyBatchModel,
CreatedModifiedModel,
UUIDModel,
):
gene_traits = models.ManyToManyField(
'GeneTrait',
through='RuleGeneTrait',
related_name='rules',
)
class Meta:
db_table = 'datasets_rules'
ordering = ['modified']
class Ruleset(
LabelModel,
BiopsyBatchModel,
CreatedModifiedModel,
UUIDModel,
):
outcome = models.ForeignKey(
'Outcome',
on_delete=models.PROTECT,
)
rules = models.ManyToManyField(
'Rule',
through='RulesetRule',
)
meta = JSONField(
blank=True,
null=True,
)
class Meta:
db_table = 'datasets_rulesets'
ordering = ['modified']
@cached_property
def rule_count(self):
return self.rules.count()
class RulesetRule(
BiopsyBatchModel,
CreatedModifiedModel,
UUIDModel,
):
ruleset = models.ForeignKey(
'Ruleset',
on_delete=models.PROTECT,
)
rule = models.ForeignKey(
'Rule',
on_delete=models.PROTECT,
)
meta = JSONField(
blank=True,
null=True,
)
class Meta:
db_table = 'datasets_rulesets_rules'
ordering = ['modified']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment