带有外键项的Scrapy Django项保存模型

时间:2019-04-03 11:14:44

标签: python django-models scrapy

我正在尝试使用eventprice_set保存Django模型,但出现错误AttributeError:eventprice_set

我尝试在模型中使用相同的代码,并且可以正常工作,但是我无法确定在抓取时这里出了什么问题

我的事件模型

class Event(models.Model):
    name = models.CharField(max_length=259, blank=False, null=False)
    place = models.ForeignKey(Place, on_delete=models.CASCADE, blank=False, null=False, verbose_name='Hosted by')
    event_cover = models.ImageField(upload_to='event-covers', blank=False, null=True)
    description = models.TextField(blank=False, null=False)
    start_date = models.DateTimeField(blank=False, null=False)
    end_date = models.DateTimeField(blank=True, null=True)
    pub_date = models.DateTimeField(default=timezone.now)
class EventPrice(models.Model):
    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    price = models.fields.DecimalField(blank=False, null=True, decimal_places=2, max_digits=5)

草率的物品代码

    def parse_khidi(self, response):
        item = EventItem(
            name=response.css('h3.eltdf-single-product-title::text').get(),
            place=Place.objects.get(name='KHIDI'),
            event_cover=response.css('img.wp-post-image').attrib['src'],
            description=str(response.css('div[id=tab-description] p::text').get()),
            start_date=self.get_start_date(response.css('h3.eltdf-single-product-title::text').get()),
            end_date=self.get_end_date(response.css('h3.eltdf-single-product-title::text').get())
        ).eventprice_set.create(price=Decimal(response.css('p.price span::text').get()))

        yield item

0 个答案:

没有答案