CablePath.origin should be unique

This commit is contained in:
Jeremy Stretch 2020-10-02 11:51:23 -04:00
parent e0abd7ef3e
commit 66355da04c
2 changed files with 9 additions and 1 deletions

View File

@ -1,3 +1,5 @@
# Generated by Django 3.1 on 2020-10-02 15:49
import dcim.fields
from django.db import migrations, models
import django.db.models.deletion
@ -18,9 +20,12 @@ class Migration(migrations.Migration):
('origin_id', models.PositiveIntegerField()),
('destination_id', models.PositiveIntegerField(blank=True, null=True)),
('path', dcim.fields.PathField(base_field=models.CharField(max_length=40), size=None)),
('is_connected', models.BooleanField(default=False)),
('destination_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
('origin_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype')),
('is_connected', models.BooleanField(default=False)),
],
options={
'unique_together': {('origin_type', 'origin_id')},
},
),
]

View File

@ -1197,6 +1197,9 @@ class CablePath(models.Model):
objects = CablePathManager()
class Meta:
unique_together = ('origin_type', 'origin_id')
def __str__(self):
path = ', '.join([str(path_node_to_object(node)) for node in self.path])
return f"Path #{self.pk}: {self.origin} to {self.destination} via ({path})"