Reputation: 93
The goal is to get the photos associated with a listing id. I have the bellow structure in 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()),
]
I keep getting the bellow errors.
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
Although I have never initiated a column listing_id_id
And
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
Sorry for the long question but I have been at it for hours. Any help is much appreciated. Cheers!
Upvotes: 1
Views: 111
Reputation: 2425
From the Database Representation [Django-doc]
Behind the scenes, Django appends "_id" to the field name to create its database column name
That's the reason why it appends _id
to your listing_id
field and it searches for listing_id_id
as column. So you don't have to add _id
while declaring Model
.
Change your Picture
model as
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'
Also change your Serializer.py
as
class PicturesSerializer(serializers.ModelSerializer):
class Meta:
model = Pictures
fields = ('index', 'listing', 'guid')
Upvotes: 2