我正在Django视图中设置一个调用API的函数,并将数据保存到我的Postgresql数据库中。
一切正常,直到得到IntegrityError slugkey already exists
,因此,我正在尝试寻找一种方法来跳过或忽略该行(如果slugify slug
已经存在)。
这是我的Django模型:
class Product(models.Model):
destination = models.CharField(max_length=255, default='')
title = models.CharField(max_length=255, default='')
slug = models.SlugField(unique=True, max_length=255)
description = models.TextField(max_length=2047, default='')
link = models.TextField(max_length=500, default='')
ptags = TaggableManager()
image = models.ImageField(max_length=500, default='images/zero-image-found.png')
timestamp = models.DateTimeField(auto_now=True)
def _ptags(self):
return [t.name for t in self.ptags.all()]
def get_absolute_url(self):
return reverse('experience',
kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(Product, self).save(*args, **kwargs)
def __str__(self):
return self.destination
这是我在“视图”中的功能:
def api_data(request):
if request.GET.get('mybtn'): # to improve, == 'something':
resp_1 = requests.get(
"https://www.headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=ROME&limit=5000¤cyCode=CAD",
headers={
"Headout-Auth": HEADOUT_PRODUCTION_API_KEY
})
resp_1_data = resp_1.json()
base_url_2 = "https://www.headout.com/api/public/v1/product/get/"
for item in resp_1_data['items']:
print('parsing, translating and saving item {}'.format(item['id']))
# concat ID to the URL string
url = '{}{}'.format(base_url_2, item['id'] + '?language=fr')
# make the HTTP request
resp_2 = requests.get(
url,
headers={
"Headout-Auth": HEADOUT_PRODUCTION_API_KEY
})
resp_2_data = resp_2.json()
try:
descriptiontxt = resp_2_data['contentListHtml'][0]['html'][0:2040] + ' ...'
except (IndexError, KeyError) as e:
continue
#Parsing the description to get only the text in <p>
soup = BeautifulSoup(descriptiontxt, 'lxml')
try:
parsed = soup.find('p').text
except AttributeError:
continue
if len(parsed) == 0:
continue
#Translation
translation = Translator().translate(text=parsed, dest='fr').text
titlename = item['name']
titlefr = Translator().translate(text=titlename, dest='fr').text
destinationname = item['city']['name']
destinationfr = Translator().translate(text=destinationname, dest='fr').text
Product.objects.get_or_create(
title=titlefr,
destination=destinationfr,
description=translation,
link=item['canonicalUrl'],
image=item['image']['url'],
)
time.sleep(2)
return render(request, "form.html")
我该如何解决?
请帮助。
答案 0 :(得分:1)
在这种情况下,我添加了数字后缀。下面的代码可以完成我肯定来自某某时间的代码。放置在您的产品模型中。
def save(self, *args, **kwargs):
# create a unique slug for this record
suffix = 0
potential = base = slugify(f'{self.title[:50]}')
self.slug = None
while not self.slug:
if suffix:
potential = f'{base}-{suffix}'
if not Product.objects.filter(slug=potential).exists():
self.slug = potential
suffix += 1
super(Product, self).save(*args, **kwargs)