我有像
这样的模特class Profile(models.Model):
id = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length=1024, blank=True, null=True)
class Meta:
managed = False
class ResourceMgmt(models.Model):
id = models.BigIntegerField(primary_key=True)
profile = models.ForeignKey(Profile, models.DO_NOTHING, related_name='cpe_mgmt_profile')
class Meta:
managed = False
序列化器
class ResourceMgmtSerializer(serializers.ModelSerializer):
profile_name = serializers.StringRelatedField(source='profile.name', read_only=True)
class Meta:
model = ResourceMgmt
fields = (<tuple of fields including profile_name>)
查看类似
class RunningSchedules(generics.ListCreateAPIView):
serializer_class = ResourceMgmtSerializer
model = ResourceMgmt
filter_backends = (filters.OrderingFilter, filters.SearchFilter,)
ordering_fields = (<tuple of fields including profile_name as profile__name>)
ordering = ('-schedule_start_time',)
search_fields = ordering_fields
现在当我点击?ordering=profile__name
这样的网址时,我收到错误ORA-00918: column ambiguously defined
。我知道这是错误,但似乎无法弄清楚为什么会发生Django ORM
更新:
这是Django == 1.8中的fixed错误,但我找到了它 在Django == 1.10。
答案 0 :(得分:0)
好的......当我重新打开ticket here Django开发人员(Simon Charette)给出一个指针时,我发现被覆盖的get_queryset
是罪魁祸首。
错误的查询是
ResourceMgmt.objects.filter(is_active='Y')\
.filter(schedule_next_run_time__lt=future)\ # future is calculated date
.filter(schedule_end_time__gt=now + datetime.timedelta(minutes=1))\
.extra(where=["schedule_next_run_time > sysdate - (schedule_frequency/1440)",
"owner_id IN ( \
select id from cpe_user \
where is_active='Y' \
connect by prior id = parent_id start with id = % s)"], 154)
已修复查询
ResourceMgmt.objects.filter(is_active='Y')\
.filter(schedule_next_run_time__lt=future)\ # future is calculated date
.filter(schedule_end_time__gt=now + datetime.timedelta(minutes=1))\
.extra(where=["schedule_next_run_time > sysdate - (schedule_frequency/1440)",
"cpe_resource_mgmt.owner_id IN ( \ ### look at this line.
select id from cpe_user \
where is_active='Y' \
connect by prior id = parent_id start with id = % s)"], 154)
正如Oracle错误所述,column ambiguously defined
,它实际上意味着owner_id
条款中的extra
。