mirror of
https://github.com/inventree/InvenTree.git
synced 2025-12-16 17:28:11 +00:00
Use GenericForeignKey
This commit is contained in:
@@ -40,14 +40,11 @@ class Migration(migrations.Migration):
|
|||||||
),
|
),
|
||||||
(
|
(
|
||||||
"model_type",
|
"model_type",
|
||||||
models.CharField(
|
models.ForeignKey(
|
||||||
blank=True,
|
blank=True,
|
||||||
default="",
|
|
||||||
help_text="Target model type for this parameter template",
|
help_text="Target model type for this parameter template",
|
||||||
max_length=100,
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
validators=[
|
to="contenttypes.contenttype",
|
||||||
common.validators.validate_parameter_template_model_type
|
|
||||||
],
|
|
||||||
verbose_name="Model type",
|
verbose_name="Model type",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -156,13 +153,9 @@ class Migration(migrations.Migration):
|
|||||||
),
|
),
|
||||||
(
|
(
|
||||||
"model_type",
|
"model_type",
|
||||||
models.CharField(
|
models.ForeignKey(
|
||||||
blank=True,
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
default="",
|
to="contenttypes.contenttype",
|
||||||
help_text="Target model type for this parameter",
|
|
||||||
max_length=100,
|
|
||||||
validators=[common.validators.validate_parameter_model_type],
|
|
||||||
verbose_name="Model type",
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@@ -224,4 +217,10 @@ class Migration(migrations.Migration):
|
|||||||
},
|
},
|
||||||
bases=(InvenTree.models.PluginValidationMixin, models.Model),
|
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"
|
||||||
|
),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2473,11 +2473,11 @@ class ParameterTemplate(
|
|||||||
|
|
||||||
return [x.strip() for x in self.choices.split(',') if x.strip()]
|
return [x.strip() for x in self.choices.split(',') if x.strip()]
|
||||||
|
|
||||||
model_type = models.CharField(
|
# TODO: Reintroduce validator for model_type
|
||||||
max_length=100,
|
model_type = models.ForeignKey(
|
||||||
default='',
|
ContentType,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
blank=True,
|
blank=True,
|
||||||
validators=[common.validators.validate_parameter_template_model_type],
|
|
||||||
verbose_name=_('Model type'),
|
verbose_name=_('Model type'),
|
||||||
help_text=_('Target model type for this parameter template'),
|
help_text=_('Target model type for this parameter template'),
|
||||||
)
|
)
|
||||||
@@ -2557,6 +2557,8 @@ class Parameter(
|
|||||||
verbose_name_plural = _('Parameters')
|
verbose_name_plural = _('Parameters')
|
||||||
unique_together = [['model_type', 'model_id', 'template']]
|
unique_together = [['model_type', 'model_id', 'template']]
|
||||||
|
|
||||||
|
indexes = [models.Index(fields=['model_type', 'model_id'])]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_api_url() -> str:
|
def get_api_url() -> str:
|
||||||
"""Return the API URL associated with the Parameter model."""
|
"""Return the API URL associated with the Parameter model."""
|
||||||
@@ -2608,6 +2610,8 @@ class Parameter(
|
|||||||
|
|
||||||
self.calculate_numeric_value()
|
self.calculate_numeric_value()
|
||||||
|
|
||||||
|
# TODO: Check that the model_type for this parameter matches the template
|
||||||
|
|
||||||
# TODO: Validation of units (check global setting)
|
# TODO: Validation of units (check global setting)
|
||||||
|
|
||||||
# TODO: Validate against plugins
|
# TODO: Validate against plugins
|
||||||
@@ -2652,20 +2656,16 @@ class Parameter(
|
|||||||
|
|
||||||
return model_class.check_related_permission(permission, user)
|
return model_class.check_related_permission(permission, user)
|
||||||
|
|
||||||
model_type = models.CharField(
|
# TODO: Reintroduce validator for model_type
|
||||||
max_length=100,
|
model_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
||||||
default='',
|
|
||||||
blank=True,
|
|
||||||
validators=[common.validators.validate_parameter_model_type],
|
|
||||||
verbose_name=_('Model type'),
|
|
||||||
help_text=_('Target model type for this parameter'),
|
|
||||||
)
|
|
||||||
|
|
||||||
model_id = models.PositiveIntegerField(
|
model_id = models.PositiveIntegerField(
|
||||||
verbose_name=_('Model ID'),
|
verbose_name=_('Model ID'),
|
||||||
help_text=_('ID of the target model for this parameter'),
|
help_text=_('ID of the target model for this parameter'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
content_object = GenericForeignKey('model_type', 'model_id')
|
||||||
|
|
||||||
template = models.ForeignKey(
|
template = models.ForeignKey(
|
||||||
ParameterTemplate,
|
ParameterTemplate,
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
|
|||||||
Reference in New Issue
Block a user