Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class System(models.Model):
- fields...
- vulnerabilities = models.ManyToManyField('Vulnerability', through='SystemVulnerability')
- class Vulnerablity(models.Model):
- name = ...
- risk = CharField(choices=['H','M','L','I'])
- ...
- class SystemVulnerability(models.Model):
- vulnerability = ForeignKey
- system = ForeignKey
- fields...
- sv_list = [199026, 199036, 199046, 199048, ....]
- other_filter = Q(... lots of stuff...) #not including this for brevity
- System.objects.filter(other_filter & Q(systemvulnerability__in=sv_list)).query.__str__()
- u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
- FROM "system"
- INNER JOIN "systemvulnerability" ON ( "system"."id" = "systemvulnerability"."system_id" )
- WHERE "systemvulnerability"."id" IN (199026, 199036, 199046, 199048, ....)'
- System.objects.filter(other_filter & ~Q(systemvulnerability__in=sv_list)).query.__str__()
- u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
- FROM "system"
- WHERE NOT ("system"."id" IN (SELECT U1."system_id" FROM "systemvulnerability" U1 WHERE U1."id" IN (199026, 199036, 199046, 199048, ....)))'
- u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
- FROM "system"
- INNER JOIN "systemvulnerability" ON ( "system"."id" = "systemvulnerability"."system_id" )
- WHERE "systemvulnerability"."id" NOT IN (199026, 199036, 199046, 199048, ....)'
- Example data
- System:
- s1
- s2
- s3
- Vulnerability:
- v1
- v2
- v3
- SystemVulnerability:
- sv1 = s1, v1
- sv2 = s1, v2
- sv3 = s2, v2
- sv4 = s3, v2
- sv5 = s3, v3
- System.filter(Q(systemvulnerability__in=[sv1, sv4]))
- Get back s1, s3: Good!
- System.filter(~Q(systemvulnerability__in=[sv1, sv2]))
- Get back: s2, s3: Good!
- System.filter(~Q(systemvulnerability__in=[sv1, sv4, sv5]))
- Get back s2: Bad!
- But I wanted s1, s2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement