I'd like to ask you a question about saving Django DB

Asked 2 years ago, Updated 2 years ago, 113 views

Hello, this is my first time developing Python and Jango web, so I am writing because I have a question. We ask for your understanding even if the level of questions is too low.

I made a web page with Django and Python for the first time

I crawled through another website and made it show the contents in the form of a bulletin board of my own.

I can't show you as it is because it's a company's business, but it's similar to making a bulletin board shown below by crawling the Naver movie information site.

The code for models.py is

from django.db import models

# # Create your models here.
class Movie(models.Model):
    movie_name = models.CharField(max_length=100)
    director = models.CharField(max_length=20)
    actor = models.CharField(max_length=3)
    grade = models.CharField(max_length=50)
    registred_dttm = models.DateTimeField(auto_now_add=True,verbose_name='time')


    def __str__(self):
        return self.project_name

This is.

And I made a Python code that made the information that crawled the web page into a list type and stored it in each table above.

import requests
from bs4 import BeautifulSoup as bs
import re
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gerrit_project.settings")
import django
django.setup()
from .models import Movie


def parse_movie():
.
.
.
.
    return movie_list
#For example, I didn't enter the code to crawl and save it because it was a Naver movie. We will make a code to return the crawled information to the list type as shown below. 
["Iron Man", "Hong Gil Dong", [Roda Ju", "Iron Man", "9.0", [Iron Man 2", "Hong Gil Dong", [Roda Ju", "Iron Man] "9.1", ....]

if __name__=='__main__':
    blog_data_dict = parse_movie()
    for i in range(len(blog_data_dict)):
        Movie(movie_name=blog_data_dict[i][0], direct=blog_data_dict[i][1], actor=blog_data_dict[i][2], grade=blog_data_dict[i][3]).save()

Now, what I'm curious about is how to update the db.

For example, when the movie's rating was different from yesterday, if you run the above file again, the existing data remains the same, and the entire crawled data accumulates again, and I changed it to update_or_create() and added one more line of the changed movie.

What I want is that when I run the file, I want all the existing data to be lost and new data to be entered or only the contents of the changed part are modified, but no matter how much I search, it doesn't come out well, so I'm asking you this question. I would appreciate it if you could let me know any good ideas or things to refer to.

djago-model

2022-09-22 13:27

1 Answers

Flying all existing data : Such an action is commonly referred to as TRUNCATE. The structure of the table is a command to initialize the content.

Only update data with fluctuations: You will need to condition update_or_create well. if X then update else create where X conditional statements are incomplete, all of them fall into create Samcheonpo.

Think more: If I were you, I would find or create a unique number for the object to crawl (in this case, the movie) and save it together in the DB. Later, when you crawl the data corresponding to the unique number, you can flip only the data from the table corresponding to the unique number by UPDATE SET with the newly crawled value.

The hill is just around the corner. Cheer up.


2022-09-22 13:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.