Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion validity/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def _config_item(self) -> Serializable:
data_file = self.data_source.datafiles.filter(path=config_path).first()
return Serializable(self.serializer, data_file=data_file)
except AttributeError as exc:
if exc.obj is not None:
# data_source may be None or, when the device is fetched without
# .prefetch_datasource()/.set_datasource(), missing altogether
missing_data_source = exc.obj is self and exc.name == "data_source"
if exc.obj is not None and not missing_data_source:
raise
return Serializable(self.serializer, data_file=None)

Expand Down
6 changes: 6 additions & 0 deletions validity/tests/test_models/test_vdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def test_config_item(create_custom_fields):
assert device._config_item() == Serializable(device.serializer, None)


def test_config_item_without_data_source(create_custom_fields):
device = DeviceFactory(name="d1")
device.serializer = SerializerDBFactory()
assert device._config_item() == Serializable(device.serializer, None)


@pytest.mark.django_db
def test_primary_ip():
vdevice = DeviceFactory()
Expand Down
Loading