I have a python function that scrapes some data from a few different websites and I want to save that data into my database only if a certain condition is met. Namely, the scraped data should only be saved if the combination of the location and date field is unique
So in my view I have a new location variable and and date variable and essentially I just need to test this combination of values against what’s already in the database. If this combination is unique, then save it. If it’s not, then do nothing.
class Speech(models.Model):
location = models.ForeignKey(Location)
speaker = models.CharField(max_lenth=100)
date = models.DateField
I’m pretty new to django so I’m just not sure how to go about executing this sort of database query.
You want a combination of two things. First, you want a inner
Metaclass to enforce the uniqueness in the database:Then, when you’re doing your data manipulation in your view, you want the
get_or_createmethod of the default model manager:I hope that gets you started. As always, you know your needs better than I do, so don’t blindly copy this example, but adapt it to your needs.
Documentation: