B
B
bogdan_242016-01-29 11:53:35
Django
bogdan_24, 2016-01-29 11:53:35

Checking if an object exists in a Django database?

there is a model:

class Music(models.Model):
    artist_mane = models.CharField(max_length=50)
    track_name = models.CharField(max_length=50)
    url = models.CharField(max_length=200)

objects of this class are created in a cycle, how to check for the presence of completely identical fields?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Klimenko, 2016-01-29
@bogdan_24

If you should not have duplicates in the database for these fields, use https://docs.djangoproject.com/en/1.9/ref/models/o...
If, in addition to this, you just want to create only what is not there:
https: //docs.djangoproject.com/en/1.9/ref/models/q...
If you want something that is not created, and the rest is updated:
https://docs.djangoproject.com/en/1.9/ref/models/ q...
ZY, One has only to take into account that there is no "magic" here, and to check the existence of objects, janga will get into the database.

D
Denis Sh, 2016-01-29
@Deq56

djbook.ru/rel1.4/ref/models/options.html#unique-to...
or rewrite the save method, which will look for the added string before saving

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question