私は似たような投稿をたくさん見てきましたが、私を助けるのに十分な情報を持っているものはありませんでした.
免責事項: 私はDjango を初めて使用します。
サブドメイン (例: jobs.mycompany.com) でホストされる私の会社の雇用申請書を作成しようとしています - これは私の最初の実際の Django プロジェクトです。そして、私がエンドユーザーのために達成したいことは、管理セクションですでに行われています.
基本的に、私は求人応募をいくつかのセクションに分割しました。
- 申請者 (いずれか 1 つだけ)
- 教育 (教育記録 [つまり、学校] - 複数可)
- Job_Experience (応募者が勤務した組織 - 複数可)
- 可用性 ( Highlander のように、1 つしか存在できません)
ここに問題があります-これを1つのフォームに正しく入れているとは思いません。また、これらすべてを一度に保存する方法もわかりません。とのフォーム セットを作成しようとEducation
しましJob_Experience
たが、それらも正しく適用されているとは思いません。
基本的に、これらすべてを表示させたいのですが、ユーザーが「送信」をクリックすると、必要なすべてのレコードが作成されます。実際に必要な部分は、申請者と空き状況だけです。
編集
繰り返しますが、管理パネルはまさに私がフロントエンドで達成したいことを行っています。、しかし(フロントエンドで)私はできませんでした:
- セレクター
applicant
入力をなくす applicant
申請者以外のモデルにインスタンスを割り当てる- 私は間違っているかもしれませんが、現在行っている方法で複数のインスタンスをビューに渡すのではなく、これらのフォームを 1 つのインスタンスに統合する方法があると思います。
以下のコード:
models.py
from django.db import models
from django import forms
from django.forms import ModelForm
from datetime import datetime
class Applicant(models.Model):
name = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
zip = models.CharField(max_length=200)
social_security_number = models.CharField(max_length=200)
phone = models.CharField(max_length=200)
alt_phone = models.CharField(max_length=200, blank=True)
us_citizen = models.BooleanField()
committed_felony = models.BooleanField()
is_16 = models.BooleanField()
has_drivers_license = models.BooleanField()
is_disabled = models.BooleanField()
prev_employed = models.BooleanField()
felony_explanation = models.TextField(blank=True)
disabled_explanation = models.TextField(blank=True)
prev_employment_manager = models.CharField(max_length=200, blank=True)
prev_employment_year = models.CharField(max_length=4, blank=True)
skills = models.TextField()
def __unicode__(self):
return self.name
class Education(models.Model):
GED = 'GED'
HIGH_SCHOOL = 'HIG'
JUNIOR_COLLEGE = 'JUN'
UNIVERSITY = 'UNI'
TYPE_OF_SCHOOL_CHOICES = (
(GED, 'GED'),
(HIGH_SCHOOL, 'High School'),
(JUNIOR_COLLEGE, 'Junior College'),
(UNIVERSITY, 'University'),
)
type = models.CharField(
max_length=3,
choices=TYPE_OF_SCHOOL_CHOICES,
default=HIGH_SCHOOL
)
school_name = models.CharField(max_length=200)
school_city = models.CharField(max_length=200)
school_state = models.CharField(max_length=200)
graduated = models.BooleanField()
graduation_year = models.CharField(max_length=4)
applicant = models.ForeignKey(Applicant)
class Job_Experience(models.Model):
FULL_TIME = 'F'
PART_TIME = 'P'
FTPT_CHOICES = (
(FULL_TIME, 'Full Time'),
(PART_TIME, 'Part Time'),
)
organization_name = models.CharField(max_length=200)
organization_city = models.CharField(max_length=200)
organization_state = models.CharField(max_length=200)
supervisor_name = models.CharField(max_length=200)
supervisor_phone = models.CharField(max_length=200)
supervisor_contact_allowed = models.BooleanField()
currently_employed = models.BooleanField()
start_date = models.DateField()
end_date = models.DateField()
starting_title = models.CharField(max_length=200)
ending_title = models.CharField(max_length=200)
start_salary = models.CharField(max_length=20)
end_salary = models.CharField(max_length=20)
reason_for_leaving = models.TextField()
full_time_part_time = models.CharField(
max_length = 1,
choices = FTPT_CHOICES,
default = PART_TIME
)
applicant = models.ForeignKey(Applicant)
class Availability (models.Model):
NOT_AVAILABLE = 'XX'
OPEN_AVAILABILITY = 'OP'
AVAILABLE_BETWEEN = 'AB'
AVAILABILITY_CHOICES = (
(NOT_AVAILABLE, 'Not Available'),
(OPEN_AVAILABILITY, 'Available All Day'),
(AVAILABLE_BETWEEN, 'Available Between Certain Hours'),
)
mon_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
mon_hours_start = models.CharField(max_length = 10)
mon_hours_end = models.CharField(max_length = 10)
tue_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
tue_hours_start = models.CharField(max_length = 10)
tue_hours_end = models.CharField(max_length = 10)
wed_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
wed_hours_start = models.CharField(max_length = 10)
wed_hours_end = models.CharField(max_length = 10)
thu_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
thu_hours_start = models.CharField(max_length = 10)
thu_hours_end = models.CharField(max_length = 10)
fri_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
fri_hours_start = models.CharField(max_length = 10)
fri_hours_end = models.CharField(max_length = 10)
fri_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
sat_hours_start = models.CharField(max_length = 10)
sat_hours_end = models.CharField(max_length = 10)
sat_availability = models.CharField(
max_length = 2,
choices = AVAILABILITY_CHOICES,
default = NOT_AVAILABLE
)
sun_hours_start = models.CharField(max_length = 10)
sun_hours_end = models.CharField(max_length = 10)
applicant = models.OneToOneField(Applicant)
# Forms
class ApplicantForm(ModelForm):
class Meta:
model = Applicant
class EducationForm(ModelForm):
class Meta:
model = Education
class JobExperienceForm(ModelForm):
class Meta:
model = Job_Experience
class AvailabilityForm(ModelForm):
class Meta:
model = Availability
ビュー.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
from applications.models import Applicant, Education, Job_Experience, Availability, ApplicantForm, EducationForm, JobExperienceForm, AvailabilityForm
from django.forms.formsets import formset_factory
def index(request):
education_formset = formset_factory(EducationForm, extra=3)
message = 'Forms have not been submitted.'
if request.method == 'POST':
applicant_form = ApplicantForm(request.POST)
education_form = education_formset(request.POST)
if applicant_form.is_valid() and education_form.is_valid():
applicant_form.save()
education_form.applicant = applicant_form
message = 'Forms are valid.'
else:
message = 'Forms are not valid.'
else:
applicant_form = ApplicantForm()
education_form = education_formset()
return render(request,
'applications/index.html',
{
'applicant_form' : applicant_form,
'education_form' : education_form,
'message' : message
}
)
admin.py
from django.contrib import admin
from applications.models import Applicant, Education, Job_Experience, Availability
class EducationInline(admin.StackedInline):
model = Education
extra = 3
class JobExperienceInline(admin.StackedInline):
model = Job_Experience
extra = 3
class AvailabilityInline(admin.StackedInline):
model = Availability
class ApplicantAdmin(admin.ModelAdmin):
inlines = [EducationInline, JobExperienceInline, AvailabilityInline]
admin.site.register(Applicant, ApplicantAdmin)
index.html
<h1>Employment Application</h1>
<p>Please enter your information into the fields below.</p>
<hr />
<p>{{ message }}</p>
<hr />
<form action="{% url 'applications:index' %}" method="post">
{% csrf_token %}
{{ applicant_form.as_p }}
<hr />
{{ education_form.as_p }}
<input type="submit" />
</form>