我有两种模式如下:
class Program(Model):
name = CharField(...)
teacher = CharField(...)
class ProgramIntake(Model):
program = ForeignKey(Program, ...)
intake = DateField(null=True, blank=True, )我有几个程序对象,例如数学和艺术作为程序模型的实例:
math = Program.objects.create(name='math', teacher='teacher1')
art = Program.objects.create(name='art', teacher='teacher2')每个程序都有几个入口:
math_intake1 = PorgramIntake.objects.create(program=math, intake='2019-01-01')
math_intake2 = PorgramIntake.objects.create(program=math, intake='2019-01-15')
math_intake3 = PorgramIntake.objects.create(program=math, intake='2020-01-01')
art_intake1 = PorgramIntake.objects.create(program=art, intake='2019-01-01')
art_intake2 = PorgramIntake.objects.create(program=art, intake='2019-02-18')
art_intake3 = PorgramIntake.objects.create(program=art, intake='2020-05-21')为了建立我的表格,我建立了这个模型:
class ProgramIntakeEnroll(Model):
available_intakes = ManyToManyField(ProgramIntake,
blank=True, ...)下面的表格是ModelForm:
class ProgramIntakeEnrollForm(ModelForm):
available_intakes = ModelMultipleChoiceField(queryset=ProgramIntakeEnroll.objects.values_list('intake', flat=True).distinct().order_by('intake'), required=False)
class Meta:
model = ProgramIntakeEnroll
fields = '__all__'本人现将此表格提交如下:
def programs(request):
template_name = 'programs/programs.html'
context = {}
if request.POST:
program_intake_enroll_form = ProgramIntakeEnrollForm(request.POST)
if study_intake_form.is_valid():
pass
else:
program_intake_enroll_form = ProgramIntakeEnrollForm()
context.update({'program_intake_enroll_form': program_intake_enroll_form,})
return render(request, template_name, context)在html中,我有:
<form method="post">
{% csrf_token %}
{{ program_intake_enroll_form.as_p }}
<button type="submit">Filter</button>
</form>我所期望的是,我只想看到以下的选择:
2019年1月
2019年2月
2020年1月
2020年5月
我想我必须更改我的ModelForm中的查询集,但是我不知道如何更改它。
发布于 2019-07-22 04:00:16
我改变了模型如下,它现在正在工作:
class IntakeRound(Model):
intake_year = PositiveSmallIntegerField(blank=True, null=True)
intake_month = PositiveSmallIntegerField(blank=True, null=True)
class Meta:
unique_together = ('intake_year', 'intake_month',)
def __str__(self):
return str(self.intake_year) + '-' + str(self.intake_month)
class Program(Model):
name = CharField(...)
intake = ManyToManyField(IntakeRound, through='ProgramIntake', blank=True)
class ProgramIntake(Model):
program = ForeignKey(Program, on_delete=CASCADE, related_name='program_intakes')
program_intake_round = ForeignKey(IntakeRound, on_delete=CASCADE, related_name='program_intakes')
program_start_date = DateField(null=True, blank=True, verbose_name='Program Start Date')
program_submission_deadline = DateField(null=True, blank=True, verbose_name='Program Submission Deadline')
class Meta:
ordering = ('program_start_date',)
def __str__(self):
return str(self.program) + '-' + str(self.program_intake_round)为了能够使用ModelForm,为了分离数据库中可用的程序和学生的兴趣,这里我也有这个模型。请注意,我不想填充它。在这个阶段,我刚刚构建了一个ModelForm:
class ProfileStudyInterest(Model):
intake_rounds_of_interest = ManyToManyField(IntakeRound, blank=True, verbose_name='Intake Rounds')这是我的表格:
class ProfileInterestFilterForm(ModelForm):
intake_rounds_of_interest = ModelMultipleChoiceField(queryset=IntakeRound.objects.all(), required=False)
class Meta:
model = ProfileStudyInterest
fields = '__all__'然而,我感到不舒服的是,我的模型中有program_start_date,而program_intake_round在同一模式下,也就是start_date的月份和年份。我认为从数据库设计的角度来看,这是不正确的或最佳做法。
https://stackoverflow.com/questions/57133510
复制相似问题