Trying to get and update or create model from json in Django
Im trying to get and update or create object in model from json list of dicts but i got an error btw my code is creating objects but cant update them:
"TypeError: Tried to update field authentication.CounterParty.GUID with a model instance, <CounterParty: CounterParty object (3)>. Use a value compatible with UUIDField. "
models.py
class CounterParty(models.Model):
GUID = models.UUIDField(default=uuid.uuid4, editable=True, unique=True)
name = models.CharField(max_length=150)
customer = models.BooleanField(default=False)
contractor = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'Counter Party'
tasks.py
def create_counter_party():
response = [
{
"CounterpartyGUID": "e58ed763-928c-4155-bee9-fdbaaadc15f3",
"CounterpartyName": "name1",
"Customer": False,
"Contractor": True
},
{
"CounterpartyGUID": "123e4567-e89b-42d3-a456-556642440000",
"CounterpartyName": "name2",
"Customer": False,
"Contractor": False
},
]
rep = response
for item in rep:
try:
counter_party = CounterParty.objects.get(GUID=item['CounterpartyGUID'])
updated_counter_party = CounterParty.objects.update(GUID=counter_party, name=item['CounterpartyName'],
customer=item['Customer'],
contractor=item['Contractor'])
updated_counter_party.save()
except CounterParty.DoesNotExist:
counter_party = CounterParty.objects.create(GUID=item['CounterpartyGUID'], name=item['CounterpartyName'],
customer=item['Customer'], contractor=item['Contractor'])
counter_party.save()
errors:
[2023-01-28 15:29:31,894: ERROR/ForkPoolWorker-8] Task authentication.tasks.create_counter_party[a86333d9-e039-479e-8d35-37fa6b17dbfa] raised unexpected: TypeError('Tried to update field authentication.CounterParty.GUID with a model
instance, <CounterParty: CounterParty object (3)>. Use a value compatible with UUIDField.')
Traceback (most recent call last):
File "/home/zesshi/.local/lib/python3.9/site-packages/celery/app/trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/zesshi/.local/lib/python3.9/site-packages/celery/app/trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "/mnt/c/Users/Shalltear/Desktop/Python/unica_django/authentication/tasks.py", line 27, in create_counter_party
counter_party = CounterParty.objects.update(GUID=counter_party, name=item['CounterpartyName'],
File "/home/zesshi/.local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/zesshi/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1191, in update
rows = query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/zesshi/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1822, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/zesshi/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1385, in execute_sql
sql, params = self.as_sql()
File "/home/zesshi/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1782, in as_sql
raise TypeError(
TypeError: Tried to update field authentication.CounterParty.GUID with a model instance, <CounterParty: CounterParty object (3)>. Use a value compatible with UUIDField.
By 2 methods you can do this
1-
try:
updated_counter_party = CounterParty.objects.create_or_update(GUID=counter_party, name=item['CounterpartyName'],
customer=item['Customer'],
contractor=item['Contractor'])
First get the CounterParty objects using id
data = CounterParty.objects.get(id=id) #change in value then save data.save()