gpt4 book ai didi

python - 无法保存模型,出现错误 : "TypeError expected string or bytes-like object"

转载 作者:行者123 更新时间:2023-12-01 02:16:55 24 4
gpt4 key购买 nike

我正在尝试从一些模型中获取信息并在模型日历中生成表条目。我无法让它保存日历条目。出现类型错误。最终,我希望这个系统根据最后安排的星期一将日历条目添加到 future 。但目前该任务仅在周一执行。但如果可能的话,我希望能够提前一周以上。

模板:

<form action="{% url 'portal:custom_admin' %}" method="post">
{% csrf_token %}
<div style="background-color:red; width:15%;">
<button name="submit">Project</button>
DANGER! Only click on mondays, until future projecter can go based off futuremost monday generated into Calendar
</div>
</form>

我正在努力的观点:

def custom_admin(request):
"""
Displays MaxSettings, also
Takes time_slots and create a week of it into the future (only should be done on mondays)
Note:eventually this should be able to generate from the last monday in the calendar instead of today
"""
max_settings = MaxSettings.objects.filter()
time_slots = TimeSlots.objects.filter()

if request.method != 'POST':
pass
# Do nothing
else:
# Project A week forward
for slot in time_slots:
#there will be an if statement for each weekday
if slot.day.weekday == 'Monday':
new_entry = Calendar(
date=datetime.timedelta(days=7),
timeslot_A=slot.timeslot_A,
timeslot_B=slot.timeslot_B,
booked=False
)
new_entry.save()
return HttpResponseRedirect(reverse('portal:custom_admin'))

calendar = Calendar.objects.filter()

context = {
'max_settings': max_settings,
'calendar': calendar,
}
return render(request, 'portal/custom_admin.html', context)

以下是相关型号:

class MaxSettings(models.Model):
"""
Everyweek day has its own specified open/close time, time slots along with number of available jobs
"""
MONDAY = 'Monday'
TUESDAY = 'Tuesday'
WEDNESDAY = 'Wednesday'
THURSDAY = 'Thursday'
FRIDAY = 'Friday'
SATURDAY = 'Saturday'
SUNDAY = 'Sunday'
WEEKDAY_CHOICES = (
(MONDAY, 'monday'),
(TUESDAY, 'tuesday'),
(WEDNESDAY, 'wednesday'),
(THURSDAY, 'thursday'),
(FRIDAY, 'friday'),
(SATURDAY, 'saturday'),
(SUNDAY, 'sunday'),
)
weekday = models.CharField(max_length=9, choices=WEEKDAY_CHOICES, )
operating = models.BooleanField()
start_work_time = models.TimeField()
end_work_time = models.TimeField()

def __str__(self):
"""Return a string representation of the model."""
return self.weekday


class TimeSlots(models.Model):
"""time slots along with number of available jobs for that slot"""
day = models.ForeignKey(MaxSettings, on_delete=models.CASCADE)
timeslot_A = models.TimeField()
timeslot_B = models.TimeField()
max_jobs = models.PositiveSmallIntegerField()

def __str__(self):
"""Return a string representation of the model."""
return '%s %s %s' % (self.timeslot_A, self.timeslot_B, self.day)


class Calendar(models.Model):
"""
this will get it details from TimeSlots and be generated into the future from the MaxSettings
"""
date = models.DateField()
timeslot_A = models.TimeField()
timeslot_B = models.TimeField()
booked = models.BooleanField()

错误详细信息:

TypeError at /custom_admin/

expected string or bytes-like object

Request Method: POST
Request URL: http://127.0.0.1:8000/custom_admin/
Django Version: 2.0
Exception Type: TypeError
Exception Value: expected string or bytes-like object

Exception Location: error location: /site-packages/django/utils/dateparse.py in parse_date, line 74

最佳答案

我的猜测是,发生错误是因为您使用 datetime.timedelta 作为日历date 属性的值> 条目。什么Django需要的是datetime.date。更准确地说,Django 需要一个日期(例如:“2018-01-17”),而您只提供两个日期之间的差异(在您的情况下:“7 天”)。您的代码应该做的是计算下周一的日期,然后将该值(转换为 datetime.date)传递给 Calendar.date 属性。

关于python - 无法保存模型,出现错误 : "TypeError expected string or bytes-like object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com