开发者

django save data in database only when certain conditions are met

开发者 https://www.devze.com 2023-04-07 07:24 出处:网络
I have a python function that scrapes some data from a few di开发者_StackOverflow社区fferent websites and I want to save that data into my database only if a certain condition is met.Namely, the scrap

I have a python function that scrapes some data from a few di开发者_StackOverflow社区fferent 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 Meta class to enforce the uniqueness in the database:

class Speech(models.Model):
    location = models.ForeignKey(Location)
    speaker = models.CharField(max_length=100)
    date = models.DateField()

    class Meta:
        unique_together = ('location', 'date')

Then, when you're doing your data manipulation in your view, you want the get_or_create method of the default model manager:

speech, new = Speech.objects.get_or_create(
    location=my_location_string,
    date=my_datetime_variable,
)

if new:
    speech.speaker = my_speaker_string
    speech.save()

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:

  • unique_together
  • get_or_create
0

精彩评论

暂无评论...
验证码 换一张
取 消