当我查询 job_seeker 个人资料时,如果没有 job_seeker 数据,则出现错误JobSeeker models query does not exist
。如果没有数据,我想传递空列表。为此,我尝试了以下方式,但出现错误,因此无法传递自定义响应
class JobSeekerNode(DjangoObjectType):
class Meta:
model = models.JobSeeker
interfaces = (relay.Node, )
class JobSeekerQueries(ObjectType):
job_seeker = Field(JobSeekerNode)
def resolve_job_seeker(self, info, **kwargs):
data = {}
if info.context.user.is_authenticated:
try:
profile = Profile.objects.get(user=info.context.user)
try:
job_seeker = models.JobSeeker.objects.get(profile=profile)
data['job_seeker'] = job_seeker
except:
# when there's no row instead of passing error, pass empty list
data['job_seeker'] = []
return JsonResponse(data)
except Profile.DoesNotExist:
return []
return None
这是我在尝试传递自定义响应时遇到的错误(如果没有数据,则为空列表)
{
"errors": [
{
"message": "Received incompatible instance \"<JsonResponse status_code=200, \"application/json\">\"."
}
],
"data": {
"job_seeker": null
}
}
我什至试过这个
def resolve_job_seeker(self, info, **kwargs):
if info.context.user.is_authenticated:
try:
profile = Profile.objects.get(user=info.context.user)
try:
job_seeker = models.JobSeeker.objects.get(profile=profile)
return job_seeker
except:
return models.JobSeeker.objects.none()
except Profile.DoesNotExist:
return []
return None
我仍然遇到这样的问题
{
"errors": [
{
"message": "Received incompatible instance \"<QuerySet []>\"."
}
],
"data": {
"job_seeker": null
}
}