我机智了。我一直在寻找类似问题的stackoverflow,但没有结果,最终放弃了解决问题的方法。
我在Django中建立了一个宠物项目,该项目使用输入表单来搜索SQLite数据库,以将农历转换为公历,反之亦然。到目前为止,一切都工作正常,但是现在我正在尝试构建链接的依赖项,其中“天”字段下拉列表仅列出所选月份的适当天数。我正在使用django-smart-selects模块来尝试完成此任务。
但是,“月份”下拉列表现在以以下形式列出了我的表格中的每个日期(表格的主键):
“月份”下拉列表应仅列出第1到12个月。“日期”下拉列表根本不会出现。我尝试删除主键字段,但随后收到以下错误消息:
django.db.utils.OperationalError: no such column: Converter.id
其中Converter是我模型中表的名称。
有人可以对此进行说明吗?
models.py
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
MONTH_RANGE_CHOICES = [(i, i) for i in range(1, 13)]
DAY_RANGE_CHOICES = [(i, i) for i in range(1, 32)]
YEAR_RANGE_CHOICES = [(i, i) for i in range(1901, 2101)]
class Month(models.Model):
name = models.IntegerField('Lunar Month', db_column='Lunar Month', choices=MONTH_RANGE_CHOICES, default='Month')
gregorian_date = models.TextField('Gregorian Date', db_column='Gregorian Date', unique=True, primary_key=True)
class Meta:
managed = False
db_table = 'Converter'
class Day(models.Model):
month = models.ForeignKey(Month, on_delete=models.CASCADE)
name = models.IntegerField(choices=DAY_RANGE_CHOICES)
class Meta:
managed = False
db_table = 'Converter'
class DateConverter(models.Model):
gregorian_date = models.TextField('Gregorian Date', db_column='Gregorian Date', unique=True, primary_key=True)
month = models.ForeignKey(Month, on_delete=models.CASCADE)
day = ChainedForeignKey(
Day,
chained_field='month',
chained_model_field='month',
show_all=False,
auto_choose=True,
sort=False
)
year = models.IntegerField('Lunar Year', db_column='Year', choices=YEAR_RANGE_CHOICES, default='Year')
class Meta:
managed = False
db_table = 'Converter'
views.py
from django.shortcuts import render, get_object_or_404, render_to_response, redirect
from django.urls import reverse
from converter.forms import LunarDateForm, GregorianDateForm
from converter.models import DateConverter
def lunar_conversion(request):
template_name = 'converter/lunar.html'
form = LunarDateForm(request.GET or None)
date = None
if form.is_valid():
date = DateConverter.objects.filter(month=form.cleaned_data.get('month'),
day=form.cleaned_data.get('day'),
year=form.cleaned_data.get('year'))\
.values_list('gregorian_date', 'day_of_week')
date = date[0]
context = {
'form': form,
'date': date,
}
return render(request, template_name, context)
forms.py
from django import forms
from converter.models import DateConverter
class LunarDateForm(forms.ModelForm):
class Meta:
model = DateConverter
fields = [
'month',
'day',
'year'
]
urls.py
from django.urls import path, include, re_path
from . import views
urlpatterns = [path('luna', views.lunar_conversion, name='lunar conversion'),
path('greg', views.gregorian_conversion, name='gregorian conversion'),
re_path(r'^chaining/', include('smart_selects.urls')),
]
html模板
{% load static %}
{% block content %}
<div class="offset-1 col-md-10 text-center" id="form">
<div class="alert alert-secondary">
<form method='GET' action=''>
{% csrf_token %}
{{ form.media.js }}
{{ form.as_p }}
<button type="submit" class="btn btn-primary btn-sm">Submit</button>
</div>
<br>
<div class="rounded p-3 mb-2 bg-light text-dark">
<p><b>Gregorian Date:</b> {{ date.0 }}</p>
<br>
<p><b>Day of the Week:</b> {{ date.1 }}</p>
<br>
</div>
</div>
{% include "converter/includes/htmlsnippet.html" %}
{% endblock %}