JYH
JYH

Reputation: 73

Django model got an unexpected keyward argument

I want to save crawling data into db, but it has unexpected keyword argument error.

reviewData() got an unexpected keyword argument 'reviewText'

some parts of crawling.py

for j in range(review_cnt):
    if soup.select_one(f'li:nth-child({j+1}) > div._1Z_GL > div.PVBo8 > a > span') != None:
        if(len(soup.select(f'li:nth-child({j+1}) > div._1Z_GL > div.PVBo8 > a > span'))) == 2:
            driver.find_element_by_css_selector(f'li:nth-child({j+1}) > div._1Z_GL > div.PVBo8 > a').send_keys(Keys.ENTER) 
            current_page = driver.page_source
            soup = BeautifulSoup(current_page, 'html.parser')  
            time.sleep(1)                                         
            review_text = soup.select_one(f'li:nth-child({j+1}) > div._1Z_GL > div.PVBo8 > a > span').text.strip() #텍스트 추출 
            star_rate = soup.select_one(f'li:nth-child({j+1}) > div._1Z_GL > div._1ZcDn > div._3D_HC > span._2tObC').text
            review_data.append((place_name, review_text, star_rate))
            review_obj = {
                'place' : place_name,
                'review' : review_text,
                'rate' : star_rate
                }
                review_dict.append(review_obj)
for item in review_dict:
  reviewData(placeName = item['place'], reviewText = item['review'], starRate = item['rate']).save() #this line has error

models.py

from django.db import models

# Create your models here.
class reviewData(models.Model):
    placeName = models.CharField(max_length=50)
    reviewText = models.TextField
    starRate = models.FloatField
    

I don't know where to fix. Should I add something more?

Upvotes: 1

Views: 63

Answers (2)

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

The fields in your model need to be instances of Field, not just a reference to the type (ie. it needs to be called with ())

class reviewData(models.Model):
    # placeName is an instance of Field which is correct (called with ())
    placeName = models.CharField(max_length=50)
    # reviewText is a reference to a type (not called with `()`)
    reviewText = models.TextField
    # starRate is also just a reference to type instead of instance
    starRate = models.FloatField

your model needs to look as follows (+applying PEP8 rules for variable names - classes are CamelCase starting with capital letters, attributes should be snake_case)

class ReviewData(models.Model):
    place_name = models.CharField(max_length=50)
    review_text = models.TextField()
    star_rate = models.FloatField()

Upvotes: 1

ysfkrcn
ysfkrcn

Reputation: 66

Maybe it's because you forgot the parenthesis

from django.db import models

# Create your models here.
class reviewData(models.Model):
    placeName = models.CharField(max_length=50)
    reviewText = models.TextField()
    starRate = models.FloatField()

Upvotes: 1

Related Questions