Django ORM JOIN postgresql type casting

632 views Asked by At

I am trying to use Django's reverse generic relation for accessing and filtering related objects via Django's ORM. See below declared models:

from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models


class Status(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Package(models.Model):
    name = models.CharField(max_length=500)
    status = models.ForeignKey(Status, on_delete=models.PROTECT)
    hst_entries = GenericRelation("myapp.StatusHistory", related_query_name="package")

    def __str__(self):
        return self.name


class StatusHistory(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
    object_id = models.TextField()
    object = GenericForeignKey("content_type", "object_id")
    status = models.ForeignKey(Status, on_delete=models.PROTECT)
    entry_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.content_type} - {self.object_id}: {self.status.name}"

As you see there is generic relation between Package and StatusHistory models. For accessing related packages from StatusHistory model I tried:

StatusHistory.objects.filter(package__name__startswith="pack")

And result:

ProgrammingError: operator does not exist: text = integer
LINE 1: ...kage" ON ("myapp_statushistory"."object_id" = "myapp...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

I understand that PostgreSQL is complaining about type mismatch between object_id (of StatusHistory model) and id (of Package model) fields. This mismatch can be solved by PostgreSQL cast operator :: as below:

### WRONG
join myapp_package on (myapp_statushistory.object_id = my_app_package.id)

### RIGHT
join myapp_package on (myapp_statushistory.object_id = my_app_package.id::varchar)

Is there possible way to make such casting via Django's ORM?

Note: Converting object_id field to IntegerField is not an option for me.

0

There are 0 answers