1
I’m starting to use Jango and its ORM and I came up with a question.
How can I check if the record exists in the database and if it does not already exist? This with only one query to the database.
I found several examples on the internet, but both end up making a second query or .count()
, and I wanted to avoid this.
I have done so (with Try and Doesnotexist), but I do not know if it is the best option since Try is intended to validate exceptions:
try:
cdfield = CadCliFieldValue.objects.filter(
cadcli_id=json_data['cadcli'],
cadcli_field_id=json_data['cadcli_field_id'],
status=1
).get()
cdfield.value = json_data['value']
cdfield.save()
except CadCliFieldValue.DoesNotExist:
cdfield = CadCliFieldValue()
cdfield.value = json_data['value']
cdfield.cadcli_id = json_data['cadcli']
cdfield.cadcli_field_id = json_data['cadcli_field_id']
cdfield.created_by = request.user
cdfield.save()
Edited: To try to explain better what I am trying to avoid, follows example of how the documentation and the examples I found on the internet recommends me to do:
cdfield = CadCliFieldValue.objects.filter(
cadcli_id=json_data['cadcli'],
cadcli_field_id=json_data['cadcli_field_id'],
status=1
)
if cdfield.exists(): # Aqui ele fez 1 consulta ao banco
cdfield = cdfield.get() # Aqui mais uma consulta (XXXX)
cdfield.value = json_data['value']
cdfield.save() # aqui um update
else:
cdfield = CadCliFieldValue()
cdfield.value = json_data['value']
cdfield.cadcli_id = json_data['cadcli']
cdfield.cadcli_field_id = json_data['cadcli_field_id']
cdfield.created_by = request.user
cdfield.save() # Aqui apenas um update
I wanted to delete the second query with get(), until I tried to take the cdfield = cdfield.get()
and just call save(), but if I don’t call the get method, python gives me that error:
'Queryset' Object has no attribute 'save'
The question seems very interesting, but I don’t understand what you want to do. Is there any way to edit the question and detail it a little more? do you want to enter a default? Avoid duplication?
– Carlos Cortez
I will edit the question, it will not be a default value, the data always comes from the form and there is the need to validate duplicities, so much so that I do this in the filters with fields that also comes from the form
– Fernando Tholl
@Fernandotholl use an index and set the column as
unique
solves the problem? If it is a set of attributes Django also provides a helper function for it. Theunique_together
. Which by the description of your problem seems to be what you need. If you have tried these approaches could explain why they do not solve your problem?– Danizavtz
@Thank you for your reply. I have no problems with the filters of my query, including if I create an Index composed of the two fields I must have the same return of Django stating that the record already exists in the bank, what I want to delete there in the second code are the two queries he makes in the bank, one to check if it exists and another to return the filled object so I can update.
– Fernando Tholl
Just complementing the comment, my first code does exactly what I want, but with Try except, I’d like another approach, without the use of except Cadclifieldvalue.Doesnotexist
– Fernando Tholl