diff --git a/python/nav/models/images.py b/python/nav/models/images.py index 827179a3b7..5b9a62febf 100644 --- a/python/nav/models/images.py +++ b/python/nav/models/images.py @@ -15,17 +15,28 @@ class Image(models.Model): id = models.AutoField(db_column='imageid', primary_key=True) room = models.ForeignKey( - Room, on_delete=models.CASCADE, db_column='roomid', null=True + Room, + on_delete=models.CASCADE, + db_column='roomid', + null=True, + related_name="images", ) location = models.ForeignKey( - Location, on_delete=models.CASCADE, db_column='locationid', null=True + Location, + on_delete=models.CASCADE, + db_column='locationid', + null=True, + related_name="images", ) title = VarcharField() path = VarcharField() name = VarcharField() created = models.DateTimeField(auto_now_add=True) uploader = models.ForeignKey( - Account, on_delete=models.CASCADE, db_column='uploader' + Account, + on_delete=models.CASCADE, + db_column='uploader', + related_name="images", ) priority = models.IntegerField() diff --git a/python/nav/models/logger.py b/python/nav/models/logger.py index 8398f8b01f..b33e0fd28f 100644 --- a/python/nav/models/logger.py +++ b/python/nav/models/logger.py @@ -46,7 +46,10 @@ class Origin(models.Model): origin = models.AutoField(db_column='origin', primary_key=True) name = VarcharField(db_column='name') category = models.ForeignKey( - LoggerCategory, on_delete=models.CASCADE, db_column='category' + LoggerCategory, + on_delete=models.CASCADE, + db_column='category', + related_name="origins", ) def __str__(self): @@ -79,7 +82,10 @@ class LogMessageType(models.Model): type = models.AutoField(db_column='type', primary_key=True) priority = models.ForeignKey( - Priority, on_delete=models.CASCADE, db_column='priority' + Priority, + on_delete=models.CASCADE, + db_column='priority', + related_name="log_message_types", ) facility = VarcharField(db_column='facility') mnemonic = VarcharField(db_column='mnemonic') @@ -101,11 +107,24 @@ class LogMessage(models.Model): id = models.AutoField(db_column='id', primary_key=True) time = models.DateTimeField(db_column='time', auto_now=True) - origin = models.ForeignKey(Origin, on_delete=models.CASCADE, db_column='origin') + origin = models.ForeignKey( + Origin, + on_delete=models.CASCADE, + db_column='origin', + related_name="log_messages", + ) newpriority = models.ForeignKey( - Priority, on_delete=models.CASCADE, db_column='newpriority' + Priority, + on_delete=models.CASCADE, + db_column='newpriority', + related_name="log_messages", + ) + type = models.ForeignKey( + LogMessageType, + on_delete=models.CASCADE, + db_column='type', + related_name="log_messages", ) - type = models.ForeignKey(LogMessageType, on_delete=models.CASCADE, db_column='type') message = VarcharField(db_column='message') class Meta(object): @@ -133,14 +152,29 @@ class MessageView(models.Model): """ origin = models.ForeignKey( - Origin, on_delete=models.CASCADE, db_column='origin', primary_key=True + Origin, + on_delete=models.CASCADE, + db_column='origin', + primary_key=True, + related_name="message_views", + ) + type = models.ForeignKey( + LogMessageType, + on_delete=models.CASCADE, + db_column='type', + related_name="message_views", ) - type = models.ForeignKey(LogMessageType, on_delete=models.CASCADE, db_column='type') newpriority = models.ForeignKey( - Priority, on_delete=models.CASCADE, db_column='newpriority' + Priority, + on_delete=models.CASCADE, + db_column='newpriority', + related_name="message_views", ) category = models.ForeignKey( - LoggerCategory, on_delete=models.CASCADE, db_column='category' + LoggerCategory, + on_delete=models.CASCADE, + db_column='category', + related_name="message_views", ) time = models.DateTimeField(db_column='time') diff --git a/python/nav/models/msgmaint.py b/python/nav/models/msgmaint.py index 8324622c19..f85e83326f 100644 --- a/python/nav/models/msgmaint.py +++ b/python/nav/models/msgmaint.py @@ -52,7 +52,10 @@ class Message(models.Model): null=True, ) maintenance_tasks = models.ManyToManyField( - 'MaintenanceTask', through='MessageToMaintenanceTask', blank=True + 'MaintenanceTask', + through='MessageToMaintenanceTask', + blank=True, + related_name="messages", ) class Meta(object): @@ -137,7 +140,7 @@ def get_components(self): """ Returns the list of model objects involved in this task """ - return [c.component for c in self.maintenancecomponent_set.all()] + return [c.component for c in self.maintenance_components.all()] def get_event_subjects(self): """ @@ -171,7 +174,10 @@ class MaintenanceComponent(models.Model): id = models.AutoField(primary_key=True) # Serial for faking primary key maintenance_task = models.ForeignKey( - MaintenanceTask, on_delete=models.CASCADE, db_column='maint_taskid' + MaintenanceTask, + on_delete=models.CASCADE, + db_column='maint_taskid', + related_name="maintenance_components", ) key = VarcharField() value = VarcharField() diff --git a/python/nav/web/info/images/utils.py b/python/nav/web/info/images/utils.py index 7b447e8e57..61b2fd7782 100644 --- a/python/nav/web/info/images/utils.py +++ b/python/nav/web/info/images/utils.py @@ -44,7 +44,7 @@ def create_hash(something, salt=False): def get_next_priority(obj): """Get the next priority value for the images in the room/location""" - priority = obj.image_set.all().aggregate(Max('priority'))['priority__max'] + priority = obj.images.all().aggregate(Max('priority'))['priority__max'] return priority + 1 if priority is not None else 0 diff --git a/python/nav/web/info/location/views.py b/python/nav/web/info/location/views.py index 83fad2971c..7e7f02a1be 100644 --- a/python/nav/web/info/location/views.py +++ b/python/nav/web/info/location/views.py @@ -102,7 +102,7 @@ def locationinfo(request, locationid): { "location": location, "navpath": navpath, - "images": location.image_set.all(), + "images": location.images.all(), "title": create_title(navpath), }, ) diff --git a/python/nav/web/info/room/views.py b/python/nav/web/info/room/views.py index fcb0eb7643..a06d98190a 100644 --- a/python/nav/web/info/room/views.py +++ b/python/nav/web/info/room/views.py @@ -123,7 +123,7 @@ def filter_netboxes(room): def roominfo(request, roomid): """Controller for displaying roominfo""" room = get_object_or_404(Room, id=roomid) - images = room.image_set.all() + images = room.images.all() navpath = get_path() + [(room.id,)] room.sorted_data = sorted(room.data.items()) room.meta_data = get_room_meta(room) diff --git a/python/nav/web/maintenance/views.py b/python/nav/web/maintenance/views.py index 01d74adb8f..fe0751ed48 100644 --- a/python/nav/web/maintenance/views.py +++ b/python/nav/web/maintenance/views.py @@ -104,7 +104,7 @@ def active(request): state__in=(MaintenanceTask.STATE_SCHEDULED, MaintenanceTask.STATE_ACTIVE), ) .order_by('-start_time', '-end_time') - .annotate(component_count=Count('maintenancecomponent')) + .annotate(component_count=Count('maintenance_components')) ) for task in tasks: # Tasks that have only one component should show a link @@ -147,7 +147,7 @@ def planned(request): state__in=(MaintenanceTask.STATE_SCHEDULED, MaintenanceTask.STATE_ACTIVE), ) .order_by('-start_time', '-end_time') - .annotate(component_count=Count('maintenancecomponent')) + .annotate(component_count=Count('maintenance_components')) ) return render( request, @@ -172,7 +172,7 @@ def historic(request): ) ) .order_by('-start_time', '-end_time') - .annotate(component_count=Count('maintenancecomponent')) + .annotate(component_count=Count('maintenance_components')) ) return render( request, @@ -270,7 +270,7 @@ def edit(request, task_id=None, start_time=None, **_): 'location': [], 'netboxgroup': [], } - for key, value in task.maintenancecomponent_set.values_list('key', 'value'): + for key, value in task.maintenance_components.values_list('key', 'value'): if key in PRIMARY_KEY_INTEGER: value = int(value) component_keys[key].append(value) diff --git a/python/nav/web/templates/info/images/upload.html b/python/nav/web/templates/info/images/upload.html index 5651e24dc3..b34283615f 100644 --- a/python/nav/web/templates/info/images/upload.html +++ b/python/nav/web/templates/info/images/upload.html @@ -13,7 +13,7 @@