You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用graphene-django过滤患者数据时遭遇弃用错误求助

解决graphene-django中'Meta.model'未搭配'fields'/'exclude'的错误

首先,咱们先定位问题根源:这个错误提示明确说,从graphene-django 0.15.0版本开始,所有继承自DjangoObjectType的类,其Meta必须明确指定fields或者exclude,不能只写model。

你贴的PatientType代码其实是符合要求的(已经设置了exclude = ('active',)),那问题大概率出在其他地方:

1. 检查其他继承的Schema中的DjangoObjectType

你的Query类继承了organization_schema.OrganizationQuery和inventory_schema.MedicineQuery,请排查这两个Schema里的所有DjangoObjectType类,确保每个类的Meta都配置了fields或exclude。比如如果其中有类似下面的代码,就会触发错误:

class SomeType(DjangoObjectType):
    class Meta:
        model = SomeModel  # 这里没加fields或exclude,就会报错

2. 确认DjangoFilterConnectionField的配置(可选)

虽然你的upcoming_appointments = DjangoFilterConnectionField(PatientType)写法本身没问题,但如果自动生成的FilterSet有潜在问题,可以手动指定FilterSet来规避:

import django_filters
from .models import Patients

class PatientFilterSet(django_filters.FilterSet):
    class Meta:
        model = Patients
        # 可以指定允许过滤的字段,或者用exclude排除不需要的
        fields = ['phone_no', 'appointments__status']

class PatientsQuery(ObjectType):
    # ... 其他字段
    upcoming_appointments = DjangoFilterConnectionField(PatientType, filterset_class=PatientFilterSet)

3. 小提示:修复resolve方法中的时区处理

顺便提一句,你代码里的settings.TIME_ZONE只是一行注释,生成aware datetime的时候最好明确指定时区,避免潜在的时区错误:

from django.utils.timezone import make_aware, get_timezone

def resolve_upcoming_appointments(self, info, **kwargs) -> List:
    d = datetime.today() - timedelta(hours=1)
    tz = get_timezone(settings.TIME_ZONE)
    aware_datetime = make_aware(d, tz)
    res = Patients.objects.filter(
        appointments__booking_date__gte=aware_datetime,
        appointments__booking_date__day=aware_datetime.day,
        appointments__status=True
    )
    return res or []

按照上面的步骤排查,应该就能解决这个弃用错误啦。

内容的提问来源于stack exchange,提问作者muhammad hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:42:53