1

我无计可施。我已经在 stackoverflow 上搜索了类似的问题,但无济于事,最终放弃了解决这个问题。

我在 django 中构建了一个宠物项目,它使用输入表单来搜索 SQLite 数据库以将农历日期转换为公历日期,反之亦然。到目前为止一切正常,但现在我正在尝试构建链式依赖项,其中日字段下拉列表仅列出所选月份的适当天数。我正在使用 django-smart-selects 模块来尝试完成此操作。

但是,“月份”下拉列表现在以以下形式列出了我的表中的每个日期(表的主键):

在此处输入图像描述

月份下拉列表应仅列出 1 - 12 个月。日下拉列表根本不填充。我尝试删除主键字段,但随后收到以下错误消息:

django.db.utils.OperationalError: no such column: Converter.id

其中 Converter 是我的模型中表的名称。

有人可以对此有所了解吗?

模型.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'

视图.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)

表格.py

from django import forms
from converter.models import DateConverter


class LunarDateForm(forms.ModelForm):
    class Meta:
        model = DateConverter
        fields = [
            'month',
            'day',
            'year'
        ]

网址.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 %}
4

0 回答 0