A正在创建一个页面,该页面将从mysql数据库中获取数据。当我尝试获取数据时,我得到了错误。
forms.py
class LeaveTrackerForm(forms.ModelForm):
emp_id = forms.CharField(max_length=50)
track = forms.CharField(max_length=50)
subtrack = forms.CharField(max_length=50,required=False)
class Meta:
model = LeaveTracker
fields = ('emp_id','track','subtrack')
models.py
class Leaves(models.Model):
legend = models.CharField(max_length=10, primary_key = True)
description = models.CharField(max_length=256)
comments = models.TextField(blank = True)
def __str__(self):
return self.legend
class Location(models.Model):
location = (
('Bangalore', 'Bangalore'),
('Chennai', 'Chennai'),
('Manila', 'Manila'),
('Mohali', 'Mohali'),
('Pune', 'Pune'),
('US', 'US'),
)
location = models.CharField(max_length=50, choices= location, primary_key = True)
def __str__(self):
return self.location
class Status(models.Model):
status = (
('active', 'Active'),
('inactive', 'Inactive'),
)
status = models.CharField(max_length=10, choices = status, primary_key=True)
def __str__(self):
return self.status
class Employee(models.Model):
emp_name = models.CharField(max_length=50)
emp_id = models.IntegerField(primary_key= True, unique = True)
email = models.EmailField()
ntid = models.CharField(max_length=15)
designation = models.CharField(max_length=20)
track = models.ForeignKey('Team', db_column = 'track', on_delete= models.CASCADE)
contact = models.CharField(max_length=13)
def __str__(self):
return "{}".format(self.emp_id,self.emp_name)
class Team(models.Model):
track = models.CharField(max_length = 50, primary_key = True)#, primary_key = True
subtrack = models.CharField(max_length=50)
offsite_lead = models.CharField(max_length=50)
onsite_lead = models.CharField(max_length=50)
status = models.ForeignKey('Status', db_column = 'status', on_delete = models.CASCADE)
location = models.ForeignKey('Location', db_column = 'location', on_delete = models.CASCADE)
def __str__(self):
return self.track
class LeaveTracker(models.Model):
DAY = (
('SUN', 'Sunday'),
('MON', 'Monday'),
('TUE', 'Tuesday'),
('WED', 'Wednesday'),
('THU', 'Thursday'),
('FRI', 'Friday'),
('SAT', 'Saturday'),
)
emp_name = models.CharField(max_length = 50)
emp_id = models.ForeignKey('Employee', db_column = 'emp_id', on_delete= models.CASCADE)
start_date = models.DateField()
end_date = models.DateField()
day = models.CharField(max_length = 10, choices = DAY)
legend = models.ForeignKey('Leaves', db_column = 'legend', on_delete = models.CASCADE)
track = models.ForeignKey('Team', db_column = 'track', on_delete = models.CASCADE)
contact = models.CharField(max_length=12)
comments = models.TextField(blank = True)
date = models.DateTimeField(auto_now = True) # it will show when the data was created in db
def __str__(self):
return "{}".format(self.emp_name)
views.py
class LeaveTrackerView(TemplateView):
template_name = 'molina/leavetracker.html'
def get(self, request):
form = LeaveTrackerForm()
posts = LeaveTracker.objects.all()
args = {'form': form, 'posts': posts}
return render(request, self.template_name, args)
def post(self, request):
form = LeaveTrackerForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.emp_name = request.emp_name
post.save()
text = form.cleaned_data['emp_id']
form = LeaveTrackerForm()
return redirect('molina:leavetracker')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
leavetracker.html
{% extends 'molina/molina_base.html' %}
{% load bootstrap3 %}
{% block content %}
<body style="background-color:powderblue;">
<div class = "container">
<p>Welcome, {{ user.get_username }}</p></div>
<h1>You are now viewing Leave Tracker</h1>
<div class="container">
<form method="post" action= {% url 'molina:leavetracker' %}>
{% csrf_token %}
{{ form.as_p }}
<button type="submit" style="background-color:green;">Submit</button>
<button type="cancel" style="background-color:green;" name=“cancel” value=“Cancel” >Cancel</button>
</form>
</div>
</body>
{% endblock %}
从数据库中获取数据时,出现以下错误 ValueError:无法分配“'656937'”:“ LeaveTracker.emp_id”必须是“ Employee”实例。
答案 0 :(得分:0)
永远不要将您的外国字段命名为some_id。 _id后缀表示您正在访问某些数据库层。 因此问题出在您的db字段上,仅将其重命名为emp。
emp = models.ForeignKey('Employee', db_column = 'emp', on_delete= models.CASCADE)