ValueError:无法将“ 1”分配给django中的“ mymodel.provider”实例,当使用scrapy将爬网数据保存到数据库时,“ mymodel.provider”必须是“ Providers”实例

时间:2019-11-25 12:47:07

标签: django django-models

当我使用scrapy将爬网数据保存到数据库时,它会显示类似

的错误
  

self.field.remote_field.model._meta.object_name,ValueError:无法分配“'1'”:“ mymodel.provider”必须是“ Providers”实例

在Django

spider.py

class CrawlSpider(scrapy.Spider):
  name = 'example'
  start_urls = ['https://example.com/'
              ]

  def parse(self, response):
      items = crawlItem()

    all_section = response.css(' div.brief_box ')
    # all_sec = response.css('div._3WlLe')
    news_provider = '1'
    # for  quotes in all_sec:
    #     dec = quotes.css('._3WlLe::text').extract()
    for quote in all_section:

        title = quote.css('.posrel img').xpath("@alt").extract()
        details = quote.css('p a').xpath('text()').extract()
        image = quote.css('.posrel img').xpath("@data-src").extract()
        page_url = quote.css('p a').xpath("@href").extract()


        items['provider'] = provider
        items['title'] = title
        items['details'] = details
        items['image'] = image
        items['page_url'] = page_url
        yield items

item.py

from scrapy_djangoitem import DjangoItem

from applications.crawl.models import Mymodel


class NewscrawlItem(DjangoItem):

  django_model = Mymodel

models.py

class Mymodel(models.Model):
    """
    model for storing news details
    """
    id = models.AutoField(primary_key=True)
    provider = models.ForeignKey(Providers, related_name='provider')
    details = models.CharField(max_length=1000, null=True, blank=True)
    page_url = models.CharField(max_length=1000, null=True, blank=True)
    image = models.CharField(max_length=1000, null=True, blank=True)
    title = models.CharField(max_length=255)


class Providers(models.Model):
    provider = models.AutoField(primary_key=True)
    url = models.CharField("Website URL", max_length=255, null=True, blank=True)
    region = models.CharField(max_length=7, choices=REGIONS, null=True, blank=True)
    image = ImageField(upload_to='provider/%Y/%m/%d/', null=True, blank=True)

1 个答案:

答案 0 :(得分:1)

正如错误消息明确指出的那样,MyModel.provider应该是Provider实例,而不是相关提供者pk的字符串表示形式。要么传递Provider实例,要么更好地传递pk,但使用正确的字段名(在大多数情况下,将使用provider_id,但是由于您的提供者模型的pk命名为provider,它可能provider_provider-但您只需要检查您的YourModel数据库模式即可了解)。