From 94d42a6d18e04e9a27d4b00c57db10a0ddc15264 Mon Sep 17 00:00:00 2001 From: d0c Date: Thu, 12 Jan 2023 18:30:28 +0100 Subject: [PATCH] queryset and object creation --- suitablephones/management/commands/fetchphones.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/suitablephones/management/commands/fetchphones.py b/suitablephones/management/commands/fetchphones.py index 4ff6e46..ac38f3f 100644 --- a/suitablephones/management/commands/fetchphones.py +++ b/suitablephones/management/commands/fetchphones.py @@ -25,16 +25,18 @@ class Command(BaseCommand): data = yaml.safe_load(stream) dev = Device() for key, value in data.items(): - print(key) if key == "bluetooth": + # get() returned more than one Bluetooth -- it returned 2! bt = Bluetooth.objects.filter(**value) + # __exact? https://docs.djangoproject.com/en/4.1/ref/models/querysets/#exact if not bt.exists(): bt = Bluetooth() for subkey, subvalue in value.items(): setattr(bt, subkey, subvalue) bt.save() - else: - dev.bluetooth = bt.get() + else: + bt = bt.get() + dev.bluetooth = bt elif key == "cameras": print('camera') # cam = []