Django api 后端过滤器 table
Django api backend filter table
目标是获取与列表 ID 关联的照片。
我在 django rest framework
中有波纹管结构
Model.py
class Pictures(models.Model):
index = models.BigIntegerField(blank=True, null=True)
listing_id = models.ForeignKey(
Listings, models.DO_NOTHING, blank=True, null=True)
post_title = models.TextField(blank=True, null=True)
guid = models.TextField(blank=True, null=True)
post_mime_type = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'pictures'
Serializer.py
class PicturesSerializer(serializers.ModelSerializer):
class Meta:
model = Pictures
fields = ('index', 'listing_id', 'guid')
Views.py
class PicturesListViewAlt(generics.ListAPIView):
serializer_class = PicturesSerializer
def get_queryset(self):
if self.request is None:
return Pictures.objects.none()
id_of_listing = self.kwargs['listing_id']
return Pictures.objects.filter(listing_id=id_of_listing)
urls.py
app_name = 'pics'
urlpatterns = [
re_path('^pictures/(?P<listing_id>.+)/$',
views.PicturesListViewAlt.as_view()),
]
我一直收到以下错误。
django.db.utils.ProgrammingError: column pictures.listing_id_id does not exist
LINE 1: ...ELECT COUNT(*) AS "__count" FROM "pictures" WHERE "pictures"...
^
HINT: Perhaps you meant to reference the column "pictures.listing_id".
[03/Feb/2022 11:58:17] "GET /api/pictures/123/ HTTP/1.1" 500 24360
虽然我从来没有发起过专栏listing_id_id
和
ProgrammingError at /api/pictures/123/
column pictures.listing_id_id does not exist
LINE 1: ...ELECT COUNT(*) AS "__count" FROM "pictures" WHERE "pictures"...
^
HINT: Perhaps you meant to reference the column "pictures.listing_id".
Which when I change my filter to pictures__listing_id I get
django.core.exceptions.FieldError: Cannot resolve keyword 'pictures' into field. Choices are: guid, id, index, listing_id, listing_id_id, post_mime_type, post_title
[03/Feb/2022 12:07:46] "GET /api/pictures/123/ HTTP/1.1" 500 22236
抱歉问了这么长的问题,但我已经问了好几个小时了。任何帮助深表感谢。干杯!
来自Database Representation [Django-doc]
Behind the scenes, Django appends "_id" to the field name to create its database column name
这就是为什么它将 _id
附加到您的 listing_id
字段并搜索 listing_id_id
作为列的原因。所以你不必在声明 Model
.
时添加 _id
将您的 Picture
模型更改为
class Pictures(models.Model):
id = models.AutoField(primary_key=True)
index = models.BigIntegerField(blank=True, null=True)
listing = models.ForeignKey(Listings, models.DO_NOTHING, blank=True, null=True)
post_title = models.TextField(blank=True, null=True)
guid = models.TextField(blank=True, null=True)
post_mime_type = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'pictures'
同时将您的 Serializer.py
更改为
class PicturesSerializer(serializers.ModelSerializer):
class Meta:
model = Pictures
fields = ('index', 'listing', 'guid')
目标是获取与列表 ID 关联的照片。 我在 django rest framework
中有波纹管结构Model.py
class Pictures(models.Model):
index = models.BigIntegerField(blank=True, null=True)
listing_id = models.ForeignKey(
Listings, models.DO_NOTHING, blank=True, null=True)
post_title = models.TextField(blank=True, null=True)
guid = models.TextField(blank=True, null=True)
post_mime_type = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'pictures'
Serializer.py
class PicturesSerializer(serializers.ModelSerializer):
class Meta:
model = Pictures
fields = ('index', 'listing_id', 'guid')
Views.py
class PicturesListViewAlt(generics.ListAPIView):
serializer_class = PicturesSerializer
def get_queryset(self):
if self.request is None:
return Pictures.objects.none()
id_of_listing = self.kwargs['listing_id']
return Pictures.objects.filter(listing_id=id_of_listing)
urls.py
app_name = 'pics'
urlpatterns = [
re_path('^pictures/(?P<listing_id>.+)/$',
views.PicturesListViewAlt.as_view()),
]
我一直收到以下错误。
django.db.utils.ProgrammingError: column pictures.listing_id_id does not exist
LINE 1: ...ELECT COUNT(*) AS "__count" FROM "pictures" WHERE "pictures"...
^
HINT: Perhaps you meant to reference the column "pictures.listing_id".
[03/Feb/2022 11:58:17] "GET /api/pictures/123/ HTTP/1.1" 500 24360
虽然我从来没有发起过专栏listing_id_id
和
ProgrammingError at /api/pictures/123/
column pictures.listing_id_id does not exist
LINE 1: ...ELECT COUNT(*) AS "__count" FROM "pictures" WHERE "pictures"...
^
HINT: Perhaps you meant to reference the column "pictures.listing_id".
Which when I change my filter to pictures__listing_id I get
django.core.exceptions.FieldError: Cannot resolve keyword 'pictures' into field. Choices are: guid, id, index, listing_id, listing_id_id, post_mime_type, post_title
[03/Feb/2022 12:07:46] "GET /api/pictures/123/ HTTP/1.1" 500 22236
抱歉问了这么长的问题,但我已经问了好几个小时了。任何帮助深表感谢。干杯!
来自Database Representation [Django-doc]
Behind the scenes, Django appends "_id" to the field name to create its database column name
这就是为什么它将 _id
附加到您的 listing_id
字段并搜索 listing_id_id
作为列的原因。所以你不必在声明 Model
.
_id
将您的 Picture
模型更改为
class Pictures(models.Model):
id = models.AutoField(primary_key=True)
index = models.BigIntegerField(blank=True, null=True)
listing = models.ForeignKey(Listings, models.DO_NOTHING, blank=True, null=True)
post_title = models.TextField(blank=True, null=True)
guid = models.TextField(blank=True, null=True)
post_mime_type = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'pictures'
同时将您的 Serializer.py
更改为
class PicturesSerializer(serializers.ModelSerializer):
class Meta:
model = Pictures
fields = ('index', 'listing', 'guid')