Skip to content

get_lookup_name doesn't work for the RelativeFIeld #124

@e-stepanov

Description

@e-stepanov

Hi! Big thanks for your really useful package.

I have faced a problem with a RelativeField. According to docs it is possible to create custom RelativeField in order to create an alias for search fields. The simplest example is the following (not tested, just example):

from djangoql.schema import RelationField, DjangoQLSchema

class CustomField(RelationField):
    name = "group_alias"

    def get_lookup_name(self):
        return "group"

class UserQLSchema(DjangoQLSchema):
    def get_fields(self, model):
        fields = super().get_fields(model)
        if model == User:
            fields += [CustomField(User, "group_alias", Group)]
        return fields


class UserQLQuerySet(DjangoQLQuerySet):
    djangoql_schema = UserQLSchema

class Group(models.Model):
    name = models.CharField()

class User(models.Model):
    group = models.ForeignKey(Group, on_delete=models.CASCADE)

The following exception is raised when I search by group_alias:

django.core.exceptions.FieldError: Cannot resolve keyword 'group_alias' into field. Choices are: list of User fields is here

After a bit of research I have realized that get_lookup_name isn't called for RelationField during name resolving. In other words we should replace item from the name.parts array with custom lookup name. If this is true I'm ready to create PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions