What I'm trying to do is saving multiple scraped data(actor names) in my Actor table inside Django models.
So far I've written the for loop below to achieve this goal but it only saves the last object.
class Actor(models.Model):
name = models.CharField(max_length=50)
url = models.URLField()
def __str__(self):
return self.name
def save(self, *args, **kwargs):
i = 0
for num in range(5):
title, year, actor_list = scraper(self.url)
self.name = actor_list[i]
super().save(*args, **kwargs)
i += 1
I get the URL from users within a form, then save the form and send it to my models and the scraping begins.
I scrape 5 actor names inside the scraper() function and append them to actor_list.
But when I try to save the actor_list under the save function, it only saves the 5th actor which I think overwrites the previously saved objects.
Is there something wrong with my for loop? or should I completely change my approach for this purpose?
I'd also prefer to save the objects using Actor.objects.get_or_create() to skip the already existing objects, but I don't know how and where.
I would appreciate it if someone could help me with this.