69👍
You can use
Entry.objects.filter(name='name', title='title').exists()
This will return to you true/false values. When you use count the orm generates query which will be executed much longer than in exists method. The get method will raise an exception when object does not exists.
request.POST is a dictionary so to check db with it you use, i.e.:
Entry.objects.filter(name=request.POST['name'], title=request.POST['title']).exists()
3👍
The answer to your question is “Yes”. However, I think you should also investigate an alternative to querying the database; create a unique key on the set of fields you don’t want duplicates to exist for.
Now, to answer your question. Check out the Django docs for making a query:
https://docs.djangoproject.com/en/dev/topics/db/queries/
In short, if you have a data model for a Thing, Thing.objects is the interface for accessing queries. from the docs (using Blog Entry, which has a string field “headline” as an example):
Entry.objects.get(headline__exact="Man bites dog")
The full capabilities of the interface are what you would expect from a database (there is a rich set of comparisons to data other than exact matches). I’d suggest looking further into the documentation for your specific problem.
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Django REST Framework: raise error when extra fields are present on POST
- [Django]-Django – how do I select a particular column from a model?