我是django的新人。
我有两个问题,
我创建了选择表单,我的第一个问题是如何将选择作为ID传递给查询?
所以我会用platform_id__in=[1,7,8] and sourcetype_id__in=[1]
platform_id__in=["platform.id"] and sourcetype_id__in=['source.id']
另一个问题是:
t_start = None
t_end = None
platform = None
source = None
如果没有这个,我得到的消息是“在赋值之前引用的局部变量”,是否有更好的方法来使视图和表单跳过这个?
这是我的代码:
模型:
class Platforms(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField()
prefix = models.CharField()
class Meta:
managed = False
db_table = 'lu_platform'
def __unicode__(self):
return self.name
class Sources(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField()
class Meta:
managed = False
db_table = 'lu_sourcetype'
def __unicode__(self):
return self.name
class Events(models.Model):
id = models.IntegerField(primary_key=True)
platform_id = models.DecimalField()
host_id = models.DecimalField()
sourcetype_id = models.DecimalField()
event_datetime = models.DateTimeField(auto_now=True)
data = models.CharField()
class Meta:
managed = False
db_table = 'event'
的观点:
def queryset(request):
t_start = None
t_end = None
platform = None
source = None
t_form = TimeSelection()
p_form = PlatformSelection()
s_form = SourceSelection()
if request.method == "POST":
t_form = TimeSelection(request.POST)
p_form = PlatformSelection(request.POST)
s_form = SourceSelection(request.POST)
if t_form.is_valid() and p_form.is_valid() and s_form.is_valid():
t_start = t_form.cleaned_data.get('t_start')
t_end = t_form.cleaned_data.get('t_end')
platform = p_form.cleaned_data.get('platforms')
source = s_form.cleaned_data.get('sources')
else:
t_form = TimeSelection()
p_form = PlatformSelection()
s_form = SourceSelection()
data = Events.objects.filter(platform_id__in=[1,7,8],
sourcetype_id__in=[1],
event_datetime__range=(t_start, t_end)
)
return render_to_response('platforms.html', {'t_form': t_form,
'p_form': p_form,
's_form': s_form,
'data': data,
't_start': t_start,
'platform': platform,
'source': source,
't_end': t_end,
},
context_instance=RequestContext(request))
形式:
class TimeSelection(forms.Form):
t_start = forms.DateField(widget=forms.DateInput(attrs={'id': 'from'}))
t_end = forms.DateField(widget=forms.DateInput(attrs={'id': 'to'}))
class PlatformSelection(forms.Form):
class Meta:
model = Platforms
platforms = forms.ModelMultipleChoiceField(queryset=Platforms.objects.all(), required=False)
class SourceSelection(forms.Form):
class Meta:
model = Sources
sources = forms.ModelMultipleChoiceField(queryset=Sources.objects.filter(pk__in=[1, 3]), required=False)
和模板:
<form method="post" action="">{% csrf_token %}
<div class="row">
<div class="col-md-6">
{{ p_form.as_table }}
</div>
<div class="col-md-6">
{{ s_form.as_table }}
</div>
</div>
<div class="col-md-12">
{{ t_form }}
<input type="submit" value="Show Charts">
</div>
</form>
答案 0 :(得分:0)
通常您希望将Choices
构建到Model
:
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
)
year_in_school = models.CharField(max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN)
def is_upperclass(self):
return self.year_in_school in (self.JUNIOR, self.SENIOR)
然后,您应该可以查询Model
,而不是在views.py