我正在尝试创建一个表单,将事件添加到归因于不同计划实例的列表中,并且我想在数据库中搞砸了一些东西。这是我的代码。
models.py
class Schedules(models.Model):
course_name = models.ForeignKey(Course)
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
room = models.CharField(max_length=128, choices=ROOM_CHOICES, default='A')
start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
start_time = models.CharField(max_length=128, choices=START_TIME_CHOICES, default='eight-thirty am')
end_time = models.CharField(max_length=128, choices=END_TIME_CHOICES, default='eight-thirty am')
instructor = models.ForeignKey(Instructor)
total_hours = models.CharField(max_length=128, choices=TOTAL_HOURS_CHOICES, default='six')
frequency = models.CharField(max_length=128)
status = models.CharField(max_length=128, choices=STATUS_CHOICES)
interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='1 day')
initiated_by = models.CharField(max_length=128, null=True)
schedule_id = models.IntegerField(default=0)
def save(self, flag=True, *args, **kwargs):
super(Schedules, self).save()
if flag:
self.schedule_id = self.id + 10000
self.save(flag=False, *args, **kwargs)
@property
def end_date(self):
days = get_days(self.interval)
return self.start_date + datetime.timedelta(days=days)
def __str__(self):
return self.course_name
class Event(models.Model):
name = models.CharField(max_length=128, choices=EVENT_CHOICES, default='final exam')
instructor = models.ForeignKey(Instructor, null=True)
schedule = models.ForeignKey(Schedules, null=True)
date = models.DateField(auto_now=False, auto_now_add=False, null=True)
start_time = models.CharField(max_length=128, null=True)
end_time = models.CharField(max_length=128, null=True)
note = models.TextField()
def __str__(self):
return self.name
forms.py
class EventForm(forms.ModelForm):
date = forms.DateField(input_formats=['%m/%d/%Y'], label="Date", widget=DateInput(format='%/m/%d/%Y'), help_text="MM/DD/YYYY")
name = forms.ChoiceField(choices=EVENT_CHOICES, initial='Final Exam', label="Event", widget=forms.Select(attrs={'class': 'form-control'}))
instructor = InstructorChoiceField(queryset=Instructor.objects.all(), label="Instructor", widget=forms.Select(attrs={'class': 'form-control'}))
start_time = forms.CharField(max_length=128, label="Start Time", widget=forms.TextInput(attrs={'class': 'form-control'}))
end_time = forms.CharField(max_length=128, label="End Time", widget=forms.TextInput(attrs={'class': 'form-control'}))
notes = forms.CharField(label="Reason", required=False, widget=forms.Textarea(attrs={'class': 'form-control'}))
class Meta:
model = Event
fields = ('name', 'instructor', 'date', 'start_time', 'end_time', 'notes')
views.py
def add_event(request, id):
try:
schedule = Schedules.objects.get(id=id)
except Schedules.DoesNotExist:
schedule = None
if request.method == 'POST':
form = EventForm(request.POST)
if form.is_valid():
if schedule:
event = form.save(commit=False)
event.schedule = schedule
event.save()
return HttpResponseRedirect('event_list', kwargs={'id':schedule.id})
else:
print(form.errors)
else:
form = EventForm()
context_dict = {'form':form, 'schedule':schedule}
return render(request, "schedule/add_event.html", context_dict)
urls.py
urlpatterns = [
url(r'^$', schedule_views.Schedule, name='schedule'),
url(r'^schedule_list/$', schedule_views.schedule_List, name='schedule_list'),
url(r'^pending_lists/$', schedule_views.pending_Lists, name='pending_lists'),
url(r'^daily_to_do/$', schedule_views.daily_To_Do, name='daily_to_do'),
url(r'^weekly_to_do/$', schedule_views.weekly_To_Do, name='weekly_to_do'),
url(r'^office_schedule/$', schedule_views.office_Schedule, name='office_schedule'),
url(r'^yearly_schedule/$', schedule_views.yearly_Schedule, name='yearly_schedule'),
url(r'^start_one_schedule/$', schedule_views.start_One_Schedule, name='start_one_schedule'),
url(r'^start_multi_schedule/$', schedule_views.start_Multi_Schedule, name='start_multi_schedule'),
url(r'^alert_emergency/$', schedule_views.alert_Emergency, name='alert_emergency'),
url(r'^instructor_admin/$', schedule_views.instructor_Admin, name='instructor_admin'),
url(r'^update_schedule/(?P<pk>\d+)$', schedule_views.update_Schedule, name='update_schedule'),
url(r'^delete_schedule/(?P<id>\d+)/$', schedule_views.delete_Schedule, name='delete_schedule'),
url(r'^search_schedule/$', schedule_views.search_Schedule, name='search_schedule'),
url(r'^event_list/(?P<id>\d+)$', schedule_views.event_list, name='event_list'),
url(r'^event_list/(?P<id>\d+)/add_event$', schedule_views.add_event, name='add_event'),
]
add_event.html
{% block main_content %}
<form id="schedule_form" method="post" action="/schedule/event_list/{{ schedule.id }}/add_event">
{% csrf_token %}
<div class="row">
{% for field in form %}
<div class="col-gl-4 col-md-4">
<div class="form-group">
<strong>{{ field.errors }}</strong>
{{ field.label_tag }}
{{ field.help_text }}
<br>
{{ field }}
<script></script>
</div>
</div>
{% endfor %}
</div>
<button type="submit" name="submit">Submit Event</button>
{% endblock %}
出于某种原因,每当我尝试提交活动时,都会收到此错误:
/ schedule / event_list / 18 / add_event
中的OperationalErrortable schedule_event没有名为name
的列我查看了SQLite中的数据库,并且出于某种奇怪的原因,事件模型表具有与“计划模型”表(first_name,last_name等)中的属性完全相同的属性,即使它们完全不同models.py。我已经迁移了一切,但它仍然存在这个问题。
另外值得注意的是,由于某种原因,manage.py要求我在Event模型中的几乎所有属性中都放置默认值,这就是为什么我必须在大多数属性中放置null = True,即使它并没有让我在其他模特中这样做。我想这可能与它有关。此外,当我迁移时,这个错误会说明错误的日期格式,但它不会停止迁移或影响项目。再次,只有这个模型。
这真的让我感到沮丧。有人能告诉我这是什么交易?感谢。
编辑:我尝试删除他们再次迁移的模型,然后将其放回并迁移它。最新的迁移在事件中具有正确的属性,但它仍然给我相同的错误,因为错误的迁移可能仍然在弄乱它。