这种情况多次发生在我身上,它无法识别我为某个对象所做的字段。 这是跟踪:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\ResNet\Desktop\Scheduler\Scheduler\Scheduler_App\models.py", line 106, in init
kevin.get_current_hours(w)
File "C:\Users\ResNet\Desktop\Scheduler\Scheduler\Scheduler_App\models.py", line 240, in get_current_hours
self.curent_hours.save()
AttributeError: 'Employee' object has no attribute 'curent_hours'
以下是models.py的大部分内容:
class Week(models.Model):
""" Base class for the workweek. """
dt = models.DateTimeField(default=timezone.now)
days = models.ManyToManyField('Day')
@classmethod
def create(cls, dt):
week = cls(dt=dt)
return week
def add_staff(self, days_availability, num_qhs, employee): # untested
for q in range(num_qhs):
qh = dt2qh(self, days_availability[q].dt)
qh.staff.add(employee)
qh.staff.save()
# self.save() -- unsure
class Day(models.Model):
""" Base class for the day. """
dt = models.DateTimeField(default=timezone.now)
total_available_qhs = models.DecimalField(default=0, max_digits=4, decimal_places=2, blank=True)
total_available_qhs.null = True
hours = models.ManyToManyField('Hour')
@classmethod
def create(cls, dt):
day = cls(dt=dt)
return day
def get_total_available_qhs(self, employees): # untested
self.total_available_qhs = employees.total_available_qhs_per_day(self.dt)
return self.total_available_qhs
class Hour(models.Model):
""" Base class for the hour. """
dt = models.DateTimeField(default=timezone.now)
qhs = models.ManyToManyField('QuarterHour')
@classmethod
def create(cls, dt):
hour = cls(dt=dt)
return hour
class QuarterHour(models.Model):
""" Base class for the quarter hour. """
dt = models.DateTimeField(default=timezone.now)
staff = models.ManyToManyField('Employee', related_name='Employee')
@classmethod
def create(cls, dt):
qh = cls(dt=dt)
return qh
def init(): # Creates the week structure
w = Week.create(datetime.datetime(2015, 7, 13))
w.save()
# print(w.days.get(dt=datetime.datetime(2015, 7, 13)).dt)
for i in range(5): # Mon - Fri
d = Day.create(w.dt + datetime.timedelta(days=i))
d.save()
for j in range(11): # 8am - 7pm
h = Hour.create(d.dt + datetime.timedelta(hours=j + 8))
h.save()
for k in range(4): # 0m, 15m, 30m, 45m
qh = QuarterHour.create(h.dt + datetime.timedelta(minutes=15 * k))
qh.save()
h.qhs.add(qh)
h.save()
d.hours.add(h)
d.save()
w.days.add(d)
w.save()
# Week structure test
'''for d in w.days.all():
for h in d.hours.all():
for qh in h.qhs.all():
print(qh.dt)'''
# dt2qh test
'''dttm = datetime.datetime(2015, 7, 14, 18, 45)
print(dttm, 'should =', dt2qh(w, dttm).dt)'''
# Employee class testing
kevin = Employee.create('kevin', 10, 2)
kevin.save()
for d in range(5):
for h in range(11):
for q in range(4):
if d > 1:
qh = w.days.all()[d].hours.all()[h].qhs.all()[q]
qh.staff.add(kevin)
qh.staff.get(name='kevin').save()
#qh.save()
kevin.get_current_hours(w)
for e in kevin.current_hours.all():
print(e.dt)
print(kevin.name)
for d in range(5):
for h in range(11):
for q in range(4):
print(w.days.all()[d].hours.all()[h].qhs.all()[q].dt)
print(w.days.all()[d].hours.all()[h].qhs.all()[q].staff.all()[0].name)
def dt2qh(week, dt): # converts from datetime to quarter hour
d = (dt - week.dt).days
h = dt.hour - 8
q = int(math.floor(dt.minute / 15))
# print(d, h, q)
return week.days.all()[d].hours.all()[h].qhs.all()[q]
class Employee(models.Model):
""" Base class for a employee."""
name = models.CharField(max_length=100)
requested_hours = models.DecimalField(default=0, max_digits=4, decimal_places=2)
requested_days = models.DecimalField(default=0, max_digits=4, decimal_places=2)
availability = models.ManyToManyField('QuarterHour', related_name='availability')
current_hours = models.ManyToManyField('QuarterHour', related_name='current_hours')
# add a+, project work, front desk booleans
def __str__(self):
return self.name
@classmethod
def create(cls, nm, rh, rd):
employee = cls(name=nm, requested_hours=rh, requested_days=rd)
return employee
def get_current_hours(self, week): # untested
for d in range(5):
for h in range(11):
for q in range(4):
qh = week.days.all()[d].hours.all()[h].qhs.all()[q]
for s in range(len(qh.staff.all())):
if qh.staff.all()[s].name == self.name and qh not in enumerate(self.current_hours.all()):
self.current_hours.add(qh)
self.curent_hours.save()
break
def get_num_current_hours(self): # untested
return len(self.current_hours.all())
def hours_fulfilled(self): # untested
return self.get_num_current_hours() >= self.requested_hours * 4
# untested
def add_availability(self, start, end, week): # start/end datetimes can only be from same day
temp = start
while temp < end:
self.availability.add(dt2qh(week, temp))
self.availability.save()
temp += datetime.timedelta(minutes=15)
def availability_for_day(self, dt): # untested
temp = []
for qh in self.availability.all():
if qh.dt.day == dt.day:
temp.append(qh)
return temp
# tries to even out peoples hours
def suggested_qhs_per_day(self): # untested
return math.floor(self.requested_hours * 4 / self.requested_days)
class Employees(models.Model):
""" Class for list of employees """
list = models.ManyToManyField('Employee')
@classmethod
def create(cls, list):
employees = cls(list=list)
return employees
def total_available_qhs_per_day(self, day): # day is a datetime
temp = 0
for e in self.list.all():
temp += len(e.availability_for_day(day))
return temp
def available_employees(self, day): # day is a datetime
temp = []
for e in self.list.all():
if len(e.availability_for_day(day)) > 0:
temp.append(e)
return temp
这是唯一的迁移:
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Day',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('dt', models.DateTimeField(default=django.utils.timezone.now)),
('total_available_qhs', models.DecimalField(default=0, blank=True, max_digits=4, decimal_places=2, null=True)),
],
),
migrations.CreateModel(
name='Employee',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('name', models.CharField(max_length=100)),
('requested_hours', models.DecimalField(default=0, max_digits=4, decimal_places=2)),
('requested_days', models.DecimalField(default=0, max_digits=4, decimal_places=2)),
],
),
migrations.CreateModel(
name='Employees',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('list', models.ManyToManyField(to='Scheduler_App.Employee')),
],
),
migrations.CreateModel(
name='Hour',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('dt', models.DateTimeField(default=django.utils.timezone.now)),
],
),
migrations.CreateModel(
name='QuarterHour',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('dt', models.DateTimeField(default=django.utils.timezone.now)),
('staff', models.ManyToManyField(to='Scheduler_App.Employee', related_name='Employee')),
],
),
migrations.CreateModel(
name='Week',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('dt', models.DateTimeField(default=django.utils.timezone.now)),
('days', models.ManyToManyField(to='Scheduler_App.Day')),
],
),
migrations.AddField(
model_name='hour',
name='qhs',
field=models.ManyToManyField(to='Scheduler_App.QuarterHour'),
),
migrations.AddField(
model_name='employee',
name='availability',
field=models.ManyToManyField(to='Scheduler_App.QuarterHour', related_name='availability'),
),
migrations.AddField(
model_name='employee',
name='current_hours',
field=models.ManyToManyField(to='Scheduler_App.QuarterHour', related_name='current_hours'),
),
migrations.AddField(
model_name='day',
name='hours',
field=models.ManyToManyField(to='Scheduler_App.Hour'),
),
]
我是django的新手,这完全阻止了我。我刚刚从sqlite3切换到mysql,因为我认为这是问题的原因,但是,唉,没用。我之前几次通过搞乱错误的字段来克服这个错误,所以它在第二次迁移中产生了AlterField,但这次没有工作。我也试过用几种方法手动修改迁移无济于事。非常感谢任何帮助,谢谢。
答案 0 :(得分:2)
您的追溯表明您遗失了r
中的current_hours
。
不应该是
self.curent_hours.save() # missing an `r` in 'current'
但是
self.current_hours.save()
请注意代码中r
中缺少current_hours
。
答案 1 :(得分:0)
你做错了。将self.curent_hours.save()更改为self.current_hours.save()