我不知道为什么子弹生成器不为表中的项目生成子弹。如果我使用.save()命令保存数据,它会完美地工作。但是它不适用于不使用Django相关命令的管道。 我使用的是Postgresql数据库
型号:
class WhiskyContent(models.Model):
products = models.CharField(max_length=250)
price = models.FloatField(default=20)
date = models.CharField(max_length=250)
picture = models.CharField(max_length=250)
site = models.CharField(max_length=250)
product_link = models.CharField(max_length=250)
shipping = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique=True, blank=True, null=True)
def get_absolute_url(self):
return "/products/{slug}/".format(slug=self.slug)
def __str__(self):
return self.products
objects = models.Manager
def product_pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = unique_slug_generator(instance)
pre_save.connect(product_pre_save_receiver, sender=WhiskyContent)
将管道插入数据库:
import psycopg2
import sys
import logging
class Frankbrauer360Pipeline(object):
def __init__(self):
self.create_connection()
def create_connection(self,):
hostname = 'localhost'
username = 'postgres'
password = 'Whisky#123!'
database = 'Items'
x = 1
try:
if (x == 1):
self.connection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)
except psycopg2.DatabaseError as e:
logging.error(e)
sys.exit()
finally:
logging.info('Connection opened successfully.')
def process_item(self, item, spider):
cursor = self.connection.cursor()
cursor.execute('''
insert into homepage_whiskycontent(products, price, picture, site, product_link, shipping, date)
values (%s, %s, %s, %s, %s, %s, %s);
''', [
item['products'],
item['price'],
item['picture'],
item['site'],
item['product_link'],
item['shipping'],
item['date'],
# item["slug"],
])
self.connection.commit()
return item
def close_spider(self, spider):
self.connection.close()
塞子发生器:
import random
import string
from django.utils.text import slugify
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def unique_slug_generator(instance, new_slug=None):
if new_slug is not None:
slug = new_slug
else:
slug = slugify(instance.products)
Klass = instance.__class__
qs_exists = Klass.objects.filter(slug=slug).exists()
if qs_exists:
new_slug = "{slug}-{randstr}".format(
slug=slug,
randstr=random_string_generator(size=4)
)
return unique_slug_generator(instance, new_slug=new_slug)
return slug
我希望有人可以帮助我:)