我的模型看起来像这样:
class Staff(models.Model):
StaffNumber = models.CharField(max_length=20,primary_key=True)
NameFirst = models.CharField(max_length=30,blank=True,null=True)
NameLast = models.CharField(max_length=30)
SchoolID = models.CharField(max_length=10,blank=True,null=True)
AutocompleteName = models.CharField(max_length=100, blank=True,null=True)
Run Code Online (Sandbox Code Playgroud)
我正在使用MySQL,以防万一.
从manage.py shell:
root@django:/var/www/django-sites/apps# python manage.py shell
Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from disciplineform.models import Staff
>>> s = Staff.objects.all()
>>> len(s)
406
Run Code Online (Sandbox Code Playgroud)
所以我知道那里有406个"Staff"对象.我也可以在数据库中看到它们.我查看其中一个值:
>>> s[0].NameFirst
u'"ANDREA"'
Run Code Online (Sandbox Code Playgroud)
这也符合我在数据库中看到的内容.现在我试着'得到'这个对象.
>>> a = Staff.objects.get(NameFirst='ANDREA')
Traceback (most recent call last):
File "<console>", line 1, in …Run Code Online (Sandbox Code Playgroud) 我是Django的新手,但我似乎在另一个网站上有相同的代码.我可以更新Django shell中的记录,但是在view.py中,相同的代码坚持在运行此表单时插入新记录.
所以view.py的相关部分是这样的:
if request.method == 'POST':
incidentId = request.POST['id']
editedEvent = DisciplineEvent.objects.get(pk=int(incidentId))
form = DisciplineEventEntryForm(request.POST, instance=editedEvent)
form.save()
variables = Context({
'account': account,
'date': request.POST['event_date'],
'description': request.POST['incident_description'],
'incident_id':incidentId,
})
template = get_template('disciplineform_confirm_entry.html')
output = template.render(variables)
response = HttpResponse(output)
return response
Run Code Online (Sandbox Code Playgroud)
我认为这会拉出有问题的记录,将新的表单数据保存到其中,并更新记录.相反,它会创建一个包含所有数据和递增主键的新记录.