DRF unable to find existing model item in API

1 day ago 3
ARTICLE AD BOX

I am creating an API with Django Rest Framework and I keep encoutering an issue where i am unable to pull a single item from my django model on the browser.

I have two endpoints one that gets the consumer by id and the other that gets ll the consumers. The one that gets all the consumers works fine and I am able to load the onto the browser but when it comes to grabbing the a single item but id which is a UUID it seems to fail or more so it keeps hitting a 404.

I have checked that the id exists in the DB and there are entries there

Views.py

@api_view(["GET"]) def get_power_consumer_by_id(request, power_consumer_id): power_consumer = get_object_or_404(PowerConsumer, id=power_consumer_id) serializer = PowerConsumerSerializer(power_consumer) return Response( serializer.data, status=status.HTTP_200_OK ) @api_view(["GET"]) def get_power_consumer(request): if request.method == "GET": try: power_consumer = PowerConsumer.objects.all() serializer = PowerConsumerSerializer(power_consumer, many=True) return Response(serializer.data, status=status.HTTP_200_OK) except Exception: return Response( {"error": "Failed to get power consumer data"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR )

Url Patterns

urlpatterns = [ path("v1/power_consumer/<uuid:power_consumer_id>/", get_power_consumer_by_id), path("v1/power_consumer/", get_power_consumer) ]

JSON Response

[{"id":"bbbbbbb2-bbbb-bbbb-bbbb-bbbbbbbbbbbb","account_number":"PC-0002","billing_mode":"MANUAL_INVOICE","billing_cycle":"MONTHLY","billing_day_of_month":20,"billing_time":"02:00:00","predicted_bill_kobo":1200000,"deficit_amount_kobo":0,"is_at_risk":false,"next_billing_at":"2026-02-20T02:00:00Z","last_billed_at":"2026-01-20T02:00:00Z","supplier":"aaaaaaa1-aaaa-aaaa-aaaa-aaaaaaaaaaaa"},{"id":"bbbbbbb3-bbbb-bbbb-bbbb-bbbbbbbbbbbb","account_number":"PC-0003","billing_mode":"AUTO_CHARGE","billing_cycle":"WEEKLY","billing_day_of_month":null,"billing_time":"02:00:00","predicted_bill_kobo":300000,"deficit_amount_kobo":50000,"is_at_risk":true,"next_billing_at":"2026-01-29T02:00:00Z","last_billed_at":"2026-01-22T02:00:00Z","supplier":"aaaaaaa1-aaaa-aaaa-aaaa-aaaaaaaaaaaa"},{"id":"bbbbbbb4-bbbb-bbbb-bbbb-bbbbbbbbbbbb","account_number":"PC-0004","billing_mode":"MANUAL_INVOICE","billing_cycle":"BIWEEKLY","billing_day_of_month":null,"billing_time":"02:00:00","predicted_bill_kobo":800000,"deficit_amount_kobo":150000,"is_at_risk":false,"next_billing_at":"2026-01-28T02:00:00Z","last_billed_at":"2026-01-14T02:00:00Z","supplier":"aaaaaaa1-aaaa-aaaa-aaaa-aaaaaaaaaaaa"},{"id":"bbbbbbb5-bbbb-bbbb-bbbb-bbbbbbbbbbbb","account_number":"PC-0005","billing_mode":"AUTO_CHARGE","billing_cycle":"DAILY","billing_day_of_month":null,"billing_time":"02:00:00","predicted_bill_kobo":45000,"deficit_amount_kobo":0,"is_at_risk":false,"next_billing_at":"2026-01-26T02:00:00Z","last_billed_at":"2026-01-25T02:00:00Z","supplier":"aaaaaaa1-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}]

Model Code

class PowerConsumer(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) supplier = models.ForeignKey(PowerSupplier, on_delete=models.CASCADE) account_number = models.CharField(max_length=100, unique=True) billing_mode = models.CharField(max_length=50) billing_cycle = models.CharField(max_length=50) billing_day_of_month = models.PositiveSmallIntegerField(null=True, blank=True) billing_time = models.TimeField(default='02:00') predicted_bill_kobo = models.BigIntegerField(default=0) deficit_amount_kobo = models.BigIntegerField(default=0) is_at_risk = models.BooleanField(default=False) next_billing_at = models.DateTimeField(null=True, blank=True) last_billed_at = models.DateTimeField(null=True, blank=True) def __str__(self): return f"Consumer {self.id}"
Read Entire Article