Python dateparser TypeError:应为字符串或类似字节的对象
这是我的自定义命令,我想将JASON likePython dateparser TypeError:应为字符串或类似字节的对象,python,json,django,dateparser,Python,Json,Django,Dateparser,这是我的自定义命令,我想将JASON likepublication date:['a week ago']中的字符串转换为真实日期,以便在Django数据库中创建它。但是当我运行命令时,我得到了这个错误。我不知道是我的命令代码还是JSON 这是一个错误: match = datetime_re.match(value) TypeError: expected string or bytes-like object 这是我的模型: from django.db import models #
publication date:['a week ago']
中的字符串转换为真实日期,以便在Django数据库中创建它。但是当我运行命令时,我得到了这个错误。我不知道是我的命令代码还是JSON
这是一个错误:
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
这是我的模型:
from django.db import models
# Create your models here.
class Job(models.Model):
job_title = models.CharField(max_length=200)
company = models.CharField(max_length=200)
company_url = models.URLField(max_length=200)
description = models.TextField()
salary = models.CharField(max_length=200)
city = models.CharField(max_length=200)
district = models.CharField(max_length=200)
publication_date = models.DateTimeField()
job_url = models.CharField(max_length=200)
job_type = models.CharField(max_length=200)
def __str__(self):
return self.job_title
多谢各位。我希望你能帮我。也许我使用dateparser或其他什么错误?我希望
dateparser.parse('publication_date')
总是返回None
。
from django.core.management.base import BaseCommand
from jobs.models import Job
import json
from datetime import datetime
import dateparser
class Command(BaseCommand):
help = 'Set up the database'
def handle(self, *args: str, **options: str):
with open('static/data.json', 'r') as handle:
big_json = json.loads(handle.read())
for item in big_json:
#Convertir fecha
publication_date = dateparser.parse('publication_date')
existing_job = Job.objects.filter(
job_title = item['job_title'],
company = item['company'],
company_url = item['company_url'],
description = item['description'],
publication_date = item['publication_date'],
salary = item['salary'],
city = item['city'],
district = item['district'],
job_url = item['job_url'],
job_type = item['job_type'],
)
if existing_job.exists() is False:
Job.objects.create(
job_title = item['job_title'],
company = item['company'],
company_url = item['company_url'],
description = item['description'],
publication_date = item['publication_date'],
salary = item['salary'],
city = item['city'],
district = item['district'],
job_url = item['job_url'],
job_type = item['job_type'],
)
self.stdout.write(self.style.SUCCESS('added jobs!'))