You are trying to save the object twice, and telling it to force the second
time, causing the error. objects.create() implicitly calls .save() When you
then try save(force_insert=True) it hits a unique error.
If you want to have the comment that is being created/saved for later use
you should do:
comment, created = Comment.objects.get_or_create(comment_title=title,
comment_content=content, comment_score=score
, comment_date=review_date,
website=website_id)
comment will be your comment and created will be a boolean - True if the
comment was created, false if it was already in the db and was just
fetched.
Kirby
On Tuesday, December 5, 2017 at 7:39:33 AM UTC-5, [email protected] wrote:
>
> I'm currently doing a web scraper app using selenium. It is supposed to
> scrape several reviews, but it won't proceed because of the error. Any
> help would be greatly appreciated.
>
> Error:
> UNIQUE constraint failed: seleniumapp_comment.id
>
> models.py:
>
> class ReviewWebsite(models.Model):
> website_name = models.CharField(max_length=50)
> website_url = models.TextField()
>
> def __str__(self):
> return self.website_name + " " + self.website_url
>
>
> class Comment(models.Model):
> website = models.ForeignKey(ReviewWebsite, on_delete=models.CASCADE)
> comment_title = models.TextField()
> comment_content = models.TextField()
> comment_score = models.CharField(max_length=7)
> comment_date = models.CharField(max_length=50)
>
>
>
> snippet of the external python script:
>
> def insert_to_db(website_name, title, content, score, review_date):
>
> if website_name == "agoda":
> site_id = 1
> elif website_name == "tripadvisor":
> site_id = 2
> else:
> site_id = 3
>
> website_id = ReviewWebsite.objects.only('id').get(id=site_id)
> comment = Comment.objects.create(comment_title=title,
> comment_content=content, comment_score=score
> , comment_date=review_date,
> website=website_id)
>
> comment.save(force_insert=True)
>
>
>
> Output:
> ===================AGODA PAGE NUMBER: 1
> Review: 1
> Title: Great food and great view!!!”
> Comment: Great food and great view!!! We will definitely come back!!!
> Score: 8.0
> Date: December 03, 2017
> Website: agoda
> UNIQUE constraint failed: seleniumapp_comment.id
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/27a31192-7941-4cf8-9f07-3c06618d5194%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.