2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-16 01:08:12 +00:00

Use GenericForeignKey

This commit is contained in:
Oliver Walters
2025-11-24 12:31:32 +00:00
parent 173839d76b
commit e9d42bd898
2 changed files with 24 additions and 25 deletions

View File

@@ -40,14 +40,11 @@ class Migration(migrations.Migration):
),
(
"model_type",
models.CharField(
models.ForeignKey(
blank=True,
default="",
help_text="Target model type for this parameter template",
max_length=100,
validators=[
common.validators.validate_parameter_template_model_type
],
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
verbose_name="Model type",
),
),
@@ -156,13 +153,9 @@ class Migration(migrations.Migration):
),
(
"model_type",
models.CharField(
blank=True,
default="",
help_text="Target model type for this parameter",
max_length=100,
validators=[common.validators.validate_parameter_model_type],
verbose_name="Model type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
(
@@ -224,4 +217,10 @@ class Migration(migrations.Migration):
},
bases=(InvenTree.models.PluginValidationMixin, models.Model),
),
migrations.AddIndex(
model_name="parameter",
index=models.Index(
fields=["model_type", "model_id"], name="common_para_model_t_244405_idx"
),
),
]

View File

@@ -2473,11 +2473,11 @@ class ParameterTemplate(
return [x.strip() for x in self.choices.split(',') if x.strip()]
model_type = models.CharField(
max_length=100,
default='',
# TODO: Reintroduce validator for model_type
model_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
blank=True,
validators=[common.validators.validate_parameter_template_model_type],
verbose_name=_('Model type'),
help_text=_('Target model type for this parameter template'),
)
@@ -2557,6 +2557,8 @@ class Parameter(
verbose_name_plural = _('Parameters')
unique_together = [['model_type', 'model_id', 'template']]
indexes = [models.Index(fields=['model_type', 'model_id'])]
@staticmethod
def get_api_url() -> str:
"""Return the API URL associated with the Parameter model."""
@@ -2608,6 +2610,8 @@ class Parameter(
self.calculate_numeric_value()
# TODO: Check that the model_type for this parameter matches the template
# TODO: Validation of units (check global setting)
# TODO: Validate against plugins
@@ -2652,20 +2656,16 @@ class Parameter(
return model_class.check_related_permission(permission, user)
model_type = models.CharField(
max_length=100,
default='',
blank=True,
validators=[common.validators.validate_parameter_model_type],
verbose_name=_('Model type'),
help_text=_('Target model type for this parameter'),
)
# TODO: Reintroduce validator for model_type
model_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
model_id = models.PositiveIntegerField(
verbose_name=_('Model ID'),
help_text=_('ID of the target model for this parameter'),
)
content_object = GenericForeignKey('model_type', 'model_id')
template = models.ForeignKey(
ParameterTemplate,
on_delete=models.CASCADE,