Compare commits

...

1 Commits

Author SHA1 Message Date
39e40d62f4 Add package parameter model.
Restructure Parameter models to use a common abstract base class.
2024-11-19 21:33:12 +01:00
3 changed files with 75 additions and 6 deletions

View File

@ -9,6 +9,7 @@ admin.site.register(parts_models.Manufacturer)
admin.site.register(parts_models.Storage)
admin.site.register(parts_models.Stock)
admin.site.register(parts_models.ComponentParameter)
admin.site.register(parts_models.PackageParameter)
admin.site.register(parts_models.ComponentParameterType)
admin.site.register(parts_models.ComponentType)
admin.site.register(parts_models.Distributor)

View File

@ -0,0 +1,49 @@
# Generated by Django 5.1.3 on 2024-11-19 20:26
import django.db.models.deletion
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("parts", "0012_storage_verbose_name"),
]
operations = [
migrations.CreateModel(
name="PackageParameter",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("value", models.FloatField(default=0)),
("text_value", models.TextField(blank=True)),
(
"package",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="parts.package"
),
),
(
"parameter_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="parts.componentparametertype",
),
),
],
options={
"ordering": ["id"],
"unique_together": {("package", "parameter_type")},
},
),
]

View File

@ -238,24 +238,25 @@ class Component(models.Model):
sum = 0
return sum
class ComponentParameter(models.Model):
class AbstractParameter(models.Model):
class Meta:
unique_together = ('component', 'parameter_type')
ordering = ['id']
abstract = True
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
component = models.ForeignKey(Component, on_delete=models.CASCADE) # A target component is required!
parameter_type = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE)
value = models.FloatField(default=0)
text_value = models.TextField(null=False, blank=True)
def _get_object_of_param(self):
return None
def __str__(self):
if self.parameter_type.parameter_type == 'F':
value = self.text_value
else:
value = str(self.value)
return str(self.component)+ ': '+ str(self.parameter_type) + ': ' + value
return str(self._get_object_of_param())+ ': '+ str(self.parameter_type) + ': ' + value
def resolved_value_as_string(self):
my_type = self.parameter_type.parameter_type
@ -270,6 +271,24 @@ class ComponentParameter(models.Model):
elif my_type == 'F':
return self.text_value
class ComponentParameter(AbstractParameter):
class Meta:
unique_together = ('component', 'parameter_type')
ordering = ['id']
component = models.ForeignKey(Component, on_delete=models.CASCADE) # A target component is required!
def _get_object_of_param(self):
return self.component
class PackageParameter(AbstractParameter):
class Meta:
unique_together = ('package', 'parameter_type')
ordering = ['id']
package = models.ForeignKey(Package, on_delete=models.CASCADE)
def _get_object_of_param(self):
return self.package
class Stock(models.Model):
class Meta:
unique_together = ('component', 'storage')