我正在努力开发一个考勤跟踪系统,我已经制作了一个"主题"具有与员工出勤相关的字段的模型。我做了一个" topicform"将这些字段显示为表单,一旦提交,详细信息将存储在"主题"表。我有另一张名为" staff"包含员工的姓名。我想编辑"名称"我的主题模型的字段到选择字段,该字段显示staff表中的所有员工,并将所选选项作为主题模型中的名称字段插入。我是Django Web开发的初学者,只需添加此功能即可使整个系统正常运行。这是我的代码:
models.py
"""
Definition of models.
"""
from django.db import models
# Create your models here.
SITES = (
('Quincy','Quincy'),
('Bowling Green','Bowling Green')
)
LEAVE_TYPE = (
('Scheduled','Scheduled'),
('Unscheduled','Unscheduled')
)
REASONS = (
('Vacation','Vacation'),
('Sick','Sick'),
('Personal','Personal'),
('Work Comp','Work Comp'),
('Holiday Float','Holiday Float'),
('PBA','PBA'),
('Military Leave','Military Leave'),
('FMLA','FMLA'),
('No Call/No Show','No Call/No Show'),
('Bereavement','Bereavement'),
)
ARRIVALS = (
('Arrived Late','Arrived Late'),
('Left Early','Left Early'),
('Arrived Late & Left Early','Arrived Late & Left Early'),
)
class Topic(models.Model):
"""A topic the user is learning about"""
date = models.DateField(blank=True, null=True)
name = models.CharField(primary_key=True, max_length=100)
site = models.CharField(max_length=100,choices=SITES, blank=True)
leave_type = models.CharField(max_length=100, choices=LEAVE_TYPE, blank=True)
reason = models.CharField(max_length=100,choices=REASONS, blank=True)
arrival = models.CharField(max_length=100,choices=ARRIVALS, blank=True)
intime = models.TimeField(blank=True, null=True)
outtime = models.TimeField(blank=True, null=True)
def __str__(self):
return self.name
return self.date
return self.site
return self.leave_type
return self.reason
return self.arrival
return self.intime
return self.outtime
views.py
"""
Definition of views.
"""
from django.shortcuts import render
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Topic
from .forms import TopicForm
def topics(request):
"""Show all topics."""
topics = Topic.objects.all()
context = {'topics': topics}
return render(request, 'app/topics.html', context)
def new_topic(request):
"""Add a new topic."""
if request.method != 'POST':
# No data submitted; create a blank form.
form = TopicForm()
else:
# POST data submitted; process data.
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('new_topic'))
context = {'form': form}
return render(request, 'app/new_topic.html', context)
def home(request):
assert isinstance(request, HttpRequest)
return render(
request,
'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
}
)
def contact(request):
"""Renders the contact page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/contact.html',
{
'title':'Contact',
'message':'Your contact page.',
'year':datetime.now().year,
}
)
def about(request):
"""Renders the about page."""
assert isinstance(request, HttpRequest)
return render(
request,
'app/about.html',
{
'title':'About',
'message':'Your application description page.',
'year':datetime.now().year,
}
)
forms.py
"""
Definition of forms.
"""
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
from .models import Topic
class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['date','name','site','leave_type','reason','arrival','intime','outtime']
widgets = {
'date': forms.TextInput(attrs={'placeholder': 'MM/DD/YYYY'}),
'intime': forms.TextInput(attrs={'placeholder': 'HH:MM:SS'}),
'outtime': forms.TextInput(attrs={'placeholder': 'HH:MM:SS'}),
}
class BootstrapAuthenticationForm(AuthenticationForm):
"""Authentication form which uses boostrap CSS."""
username = forms.CharField(max_length=254,
widget=forms.TextInput({
'class': 'form-control',
'placeholder': 'User name'}))
password = forms.CharField(label=_("Password"),
widget=forms.PasswordInput({
'class': 'form-control',
'placeholder':'Password'})
)
如何添加从我的SQLServer数据库中提取员工姓名并将其设置为我的主题模型中的名称字段的人员模型?