ivelum / djangoql

Advanced search language for Django
MIT License
1.02k stars 89 forks source link

Related names #106

Closed sdas41395 closed 1 year ago

sdas41395 commented 1 year ago

Not sure if is still being maintained but in schema.py there is a discrepancy with related names and how we get fields for autocomplete here.

_meta.get_fields will return related names in addition to the model fields.

  def get_fields(self, model):
        """
        By default, returns all field names of a given model.

        Override this method to limit field options. You can either return a
        plain list of field names from it, like ['id', 'name'], or call
        .super() and exclude unwanted fields from its result.
        """
        return sorted(
            [f.name for f in model._meta.get_fields() if f.name != 'password'],
        )

However when this function is used in introspect line 366:

         model_fields = OrderedDict()
         for field in self.get_fields(model):
                if not isinstance(field, DjangoQLField):
                    field = self.get_field_instance(model, field)

the get_field_instance function will call meta.get_field which does not support related names. This breaks the query language search.

    def get_field_instance(self, model, field_name):
        field = model._meta.get_field(field_name)
        field_kwargs = {'model': model, 'name': field.name}
       .....

I think we would need to use get_attr or make sure we handle related names