无法获取要保存的模型获取错误:“TypeError应为字符串或byteslike对象”

2024-09-26 17:47:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一些模型中获取信息并在模型日历中生成表条目。我无法让它保存日历条目。输入错误。最后,我希望这个系统能在最后一个星期一的基础上添加日历条目。但目前这项任务只在周一执行。但如果可能的话,我希望能提前一周以上。在

模板:

<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>

我正在挣扎的观点:

^{pr2}$

以下是相关车型:

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

Tags: oftheselfstringmodeladmintimemodels
1条回答
网友
1楼 · 发布于 2024-09-26 17:47:01

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

相关问题 更多 >

    热门问题