Repository: netbox-community/netbox

Issue:
Unable to sort VMs by primary_ip
<!--
    NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED.

    This form is only for reporting reproducible bugs. If you need assistance
    with NetBox installation, or if you have a general question, please start a
    discussion instead: https://github.com/netbox-community/netbox/discussions

    Please describe the environment in which you are running NetBox. Be sure
    that you are running an unmodified instance of the latest stable release
    before submitting a bug report, and that any plugins have been disabled.
-->
### Environment
* Python version: 3.6.8
* NetBox version: 2.10.4

<!--
    Describe in detail the exact steps that someone else can take to reproduce
    this bug using the current stable release of NetBox. Begin with the
    creation of any necessary database objects and call out every operation
    being performed explicitly. If reporting a bug in the REST API, be sure to
    reconstruct the raw HTTP request(s) being made: Don't rely on a client
    library such as pynetbox.
-->
### Steps to Reproduce
1. Sort virtual machines by IP Address
https://[address]/virtualization/virtual-machines/?sort=primary_ip

<!-- What did you expect to happen? -->
### Expected Behavior
VMs to be sorted.

<!-- What happened instead? -->
### Observed Behavior
Application crash.

```
<class 'django.core.exceptions.FieldError'>

Cannot resolve keyword 'primary_ip' into field. Choices are: cluster, cluster_id, comments, created, custom_field_data, disk, id, interfaces, last_updated, local_context_data, memory, name, platform, platform_id, primary_ip4, primary_ip4_id, primary_ip6, primary_ip6_id, role, role_id, secrets, services, status, tagged_items, tags, tenant, tenant_id, vcpus

Python version: 3.6.8
NetBox version: 2.10.4
```

Same bug as #5597 


Discussion:

Resolving patch:
diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py
--- a/netbox/dcim/tables/devices.py
+++ b/netbox/dcim/tables/devices.py
@@ -1,5 +1,6 @@
 import django_tables2 as tables
 from django_tables2.utils import Accessor
+from django.conf import settings
 
 from dcim.models import (
     ConsolePort, ConsoleServerPort, Device, DeviceBay, DeviceRole, FrontPort, Interface, InventoryItem, Platform,
@@ -127,11 +128,18 @@ class DeviceTable(BaseTable):
         verbose_name='Type',
         text=lambda record: record.device_type.display_name
     )
-    primary_ip = tables.Column(
-        linkify=True,
-        order_by=('primary_ip6', 'primary_ip4'),
-        verbose_name='IP Address'
-    )
+    if settings.PREFER_IPV4:
+        primary_ip = tables.Column(
+            linkify=True,
+            order_by=('primary_ip4', 'primary_ip6'),
+            verbose_name='IP Address'
+        )
+    else:
+        primary_ip = tables.Column(
+            linkify=True,
+            order_by=('primary_ip6', 'primary_ip4'),
+            verbose_name='IP Address'
+        )
     primary_ip4 = tables.Column(
         linkify=True,
         verbose_name='IPv4 Address'
diff --git a/netbox/virtualization/tables.py b/netbox/virtualization/tables.py
--- a/netbox/virtualization/tables.py
+++ b/netbox/virtualization/tables.py
@@ -1,5 +1,5 @@
 import django_tables2 as tables
-
+from django.conf import settings
 from dcim.tables.devices import BaseInterfaceTable
 from tenancy.tables import COL_TENANT
 from utilities.tables import (
@@ -125,10 +125,18 @@ class VirtualMachineDetailTable(VirtualMachineTable):
         linkify=True,
         verbose_name='IPv6 Address'
     )
-    primary_ip = tables.Column(
-        linkify=True,
-        verbose_name='IP Address'
-    )
+    if settings.PREFER_IPV4:
+        primary_ip = tables.Column(
+            linkify=True,
+            order_by=('primary_ip4', 'primary_ip6'),
+            verbose_name='IP Address'
+        )
+    else:
+        primary_ip = tables.Column(
+            linkify=True,
+            order_by=('primary_ip6', 'primary_ip4'),
+            verbose_name='IP Address'
+        )
     tags = TagColumn(
         url_name='virtualization:virtualmachine_list'
     )


Test patch: