0
我有一個小問題,要求社區。我想知道爲什麼當我去編輯表單時我的外鍵不會顯示出來。當我去編輯信息時,除了狀態(這是一個從狀態表指向項目表的外鍵),我的所有數據都被填充,但沒有任何選擇。我使用相同的forms.py來添加和編輯信息。FK不填充在Django的形式
models.py
from django.db import models
from clients.models import Clients
from django.contrib.auth.models import User
from settings import STATUS_CHOICES
class Project(models.Model):
client = models.ForeignKey(Clients, related_name='projects')
created_by = models.ForeignKey(User, related_name='created_by')
#general information
proj_name = models.CharField(max_length=255, verbose_name='Project Name')
pre_quote = models.CharField(max_length=3,default='10-')
quote = models.IntegerField(max_length=10, verbose_name='Quote #', unique=True)
desc = models.TextField(verbose_name='Description')
starts_on = models.DateField(verbose_name='Start Date')
completed_on = models.DateField(verbose_name='Finished On')
def __unicode__(self):
return u'%s' % (self.proj_name)
def current_status(self):
try:
return self.status.all().order_by('-id')[:1][0]
except:
return None
class Status(models.Model):
project = models.ForeignKey(Project, related_name='status')
value = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='Status')
date_created= models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.value
class Meta:
verbose_name = ('Status')
verbose_name_plural = ("Status")
views.py
@login_required
def addProject(request):
if request.method == 'POST':
form = AddSingleProjectForm(request.POST)
if form.is_valid():
project = form.save(commit=False)
project.created_by = request.user
project.save()
project.status.create(
value = form.cleaned_data.get('status', None)
)
return HttpResponseRedirect('/project/')
else:
form = AddSingleProjectForm()
return render_to_response('project/addProject.html', {
'form': form, 'user':request.user}, context_instance=RequestContext(request))
@login_required
def editProject(request, proj_id):
proj = Project.objects.get(pk=proj_id)
if request.method == 'POST':
form = AddSingleProjectForm(request.POST,instance=proj)
if form.is_valid():
form.save()
return HttpResponseRedirect('/project/')
else:
form = AddSingleProjectForm(instance=proj)
return render_to_response('project/edit_project.html', {
'form': form}, context_instance=RequestContext(request))
forms.py
from django.db import models
from project.models import Project, Status
from django.forms import *
from django import forms
from settings import STATUS_CHOICES
class AddSingleProjectForm(ModelForm):
status = forms.ChoiceField(choices=STATUS_CHOICES)
class Meta:
model = Project
exclude = ('pre_quote', 'created_by')
從editproject模板
段:
{% block content %}
<FORM METHOD="POST" ACTION="">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Save Project">
</FORM>
{% endblock %}
任何建議將不勝感激。
謝謝。
好吧,如果我手動添加到表單,我會在哪裏開始呢?對不起,我還是這個新手。感謝您及時的回覆! – TheLifeOfSteve 2010-10-19 20:13:16
嘗試覆蓋'__init__'的形式。如果'__init__'獲得'instance',你會發現狀態('s = Status.objects.filter(project = instance).order_by(' - date_created')[0]'),並將初始值添加到self中。 status.initial = s.value'。我希望這有效(不確定)。 – OmerGertel 2010-10-19 20:24:27
我加了: def __init __(self,* args,** kwargs): super(AddSingleProjectForm,self).__ init __(* args,** kwargs) self.fields ['status']。initial = self.instance .current_status() 我的forms.py,並允許我把它拉到editform。謝謝你的幫助! – TheLifeOfSteve 2010-10-20 20:18:10