나는 공식 장고 자습서에서 작성된 투표소 응용 프로그램에 더 많은 기능을 추가하려고합니다. 내가하고있는 일 중 하나는 로그인 한 사용자 (설문서가 우리를 떠나는 관리자 화면 대신)로 설문 조사/선택 사항을 생성 할 수 있도록하는 것입니다.하나의 장고보기에서 두 양식 결합하기
사용자가 설문 조사를 작성하고 설문 조사에 연결할 수있는 선택 사항을 포함 할 수있는보기를 만들려고합니다. 장고 관리자가 자동으로 그것을 수행하고, 나는 이것을보기에 쓰는 방법에 대해 확신 할 수 없다. ,
models.py
import datetime
from django.db import models
from django.utils import timezone
class Poll(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
question = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
forms.py 현재
from django import forms
from .models import Poll, Choice
from datetime import datetime
class PollForm(forms.ModelForm):
question_text = forms.CharField(max_length=200, help_text="Please enter the question.")
pub_date = forms.DateTimeField(widget=forms.HiddenInput(), initial = datetime.now())
class Meta:
model = Poll
fields = ("__all__")
class ChoiceForm(forms.ModelForm):
choice_text = forms.CharField(max_length=200, help_text="Please enter choices.")
votes = forms.IntegerField(widget=forms.HiddenInput(),initial=0)
exclude = ('poll',)
views.py
def add_poll(request):
# A HTTP POST?
if request.method == 'POST':
form = PollForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
return render(request, 'polls/index.html', {})
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = PollForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render(request, 'polls/add_poll.html', {'form': form})
: 시작하기
이 내 관련 파일입니다 내보기를 사용하면 사용자가 설문 조사. 입력 된 텍스트를 Poll 모델의 question_text, Choice 모델 및 ChoiceForm으로 전달하는 방법에 대해서는 확신 할 수 없습니다.
모든 안내 및 권장 사항은 언제나 감사하겠습니다!
건배, 폴
당신은 이것을 보았습니까? http://stackoverflow.com/questions/2770810/multiple-models-in-a-single-django-modelform –