简体   繁体   中英

how do I fix PEP fields.E339 in django models through/through_fields?

I am trying to link vulnerability cve models with products via intermediate models to link/match each vulnerability based on the product / cve

class Vulnerability(models.Model):
    cveid = models.CharField(max_length=32, null=True, blank=True)
    affected_products = models.ManyToManyField(
        Product,
        through='ProductVulnerability',
        through_fields=("product", "vulnerability")
    )


class ProductVulnerability(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    vulnerability = models.ForeignKey(Vulnerability, on_delete=models.CASCADE)
    
    class Meta:
        unique_together = [("product", "vulnerability")]
service         | ERRORS:
service         | api.Vulnerability.affected_products: (fields.E339) 'ProductVulnerability.product' is not a foreign key to 'Vulnerability'.
service         |   HINT: Did you mean one of the following foreign keys to 'Vulnerability': vulnerability?
service         | api.Vulnerability.affected_products: (fields.E339) 'ProductVulnerability.vulnerability' is not a foreign key to 'Product'.
service         |   HINT: Did you mean one of the following foreign keys to 'Product': product?
service         | SystemCheckError: System check identified some issues:

As documented and as HINT would suggest

through_fields accepts a 2-tuple ('field1', 'field2'), where field1 is the name of the foreign key to the model the ManyToManyField is defined on, and field2 the name of the foreign key to the target model.

so in your case

affected_products = models.ManyToManyField(
    Product,
    through='ProductVulnerability',
    through_fields=("vulnerability", "product")
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM