问题
I have am trying to create the model class on which i want to add the extra field with ManytoMany field. On some google search I have identified its possible to through
on implementing I am getting couple of errors which i am not able to identify whats the issue. Here is my model class:
class CompQuantity(models.Model):
comp_code = models.ForeignKey(
'core.SensorDevice',
on_delete=models.CASCADE,
null=True
)
quantity = models.IntegerField()
class PackageComponent(models.Model):
pkg_code = models.CharField(max_length=255)
package = models.ManyToManyField(
'core.Component',
through='CompQuantity',
through_fields=('comp_code', 'quantity')
)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
What I am trying to achieve here in PackageComponent I want to create the package as ManytoMany field relation with Component table with the another field Quantity. But I am getting the following errors as:
ERRORS:
oss.CompQuantity: (fields.E336) The model is used as an intermediate model by 'oss.PackageComponent.package', but it does not have a foreign key to 'PackageComponent' or 'Component'.
oss.PackageComponent.package: (fields.E339) 'CompQuantity.comp_code' is not a foreign key to 'PackageComponent'.
oss.PackageComponent.package: (fields.E339) 'CompQuantity.quantity' is not a foreign key to 'SensorDevice'.
HINT: Did you mean one of the following foreign keys to 'Component': comp_code?
I am not able to understand why I am getting this error. Any help will be highly appreciated. Thank you
来源:https://stackoverflow.com/questions/63169448/add-extra-field-with-manytomany-field-in-drf