pgvector / pgvector-python

pgvector support for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integration with Django Admin

ethagnawl opened this issue · comments

When attempting to create database records in a Django Admin application, pgvector values fail the basic, naive admin form validation with:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Is there a common approach for working around this at the model (preferable) or admin form level? I've tried specifying a validator function via the model field's validators kwarg and the first round of "truthy" validation (i.e. the one referenced above) fails before my function is called.

It seems to be the package is not supported via forms and templates. The issue can be reproduced with the below setup.

# models.py

from pgvector.django import VectorField


class Vector(models.Model):
    test = VectorField(dimensions=3)

    def __str__(self):
        return str(self.test)
# forms.py

from django import forms


class VectorForm(forms.ModelForm):
    class Meta:
        model = Vector
        fields = ["test"]

Execution

vf = VectorForm(data={"test":[1,2,3]})
vf.is_valid()

Traceback

ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 vf.is_valid()

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/forms/forms.py:201, in BaseForm.is_valid(self)
    199 def is_valid(self):
    200     """Return True if the form has no errors, or False otherwise."""
--> 201     return self.is_bound and not self.errors

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/forms/forms.py:196, in BaseForm.errors(self)
    194 """Return an ErrorDict for the data provided for the form."""
    195 if self._errors is None:
--> 196     self.full_clean()
    197 return self._errors

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/forms/forms.py:435, in BaseForm.full_clean(self)
    433 self._clean_fields()
    434 self._clean_form()
--> 435 self._post_clean()

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/forms/models.py:486, in BaseModelForm._post_clean(self)
    483     self._update_errors(e)
    485 try:
--> 486     self.instance.full_clean(exclude=exclude, validate_unique=False)
    487 except ValidationError as e:
    488     self._update_errors(e)

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/db/models/base.py:1470, in Model.full_clean(self, exclude, validate_unique, validate_constraints)
   1467     exclude = set(exclude)
   1469 try:
-> 1470     self.clean_fields(exclude=exclude)
   1471 except ValidationError as e:
   1472     errors = e.update_error_dict(errors)

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/db/models/base.py:1522, in Model.clean_fields(self, exclude)
   1520     continue
   1521 try:
-> 1522     setattr(self, f.attname, f.clean(raw_value, self))
   1523 except ValidationError as e:
   1524     errors[f.name] = e.error_list

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/db/models/fields/__init__.py:777, in Field.clean(self, value, model_instance)
    771 """
    772 Convert the value's type and run validation. Validation errors
    773 from to_python() and validate() are propagated. Return the correct
    774 value if no error is raised.
    775 """
    776 value = self.to_python(value)
--> 777 self.validate(value, model_instance)
    778 self.run_validators(value)
    779 return value

File ~/.local/share/virtualenvs/generic-django-example-Kc6WXfau/lib/python3.9/site-packages/django/db/models/fields/__init__.py:767, in Field.validate(self, value, model_instance)
    764 if value is None and not self.null:
    765     raise exceptions.ValidationError(self.error_messages["null"], code="null")
--> 767 if not self.blank and value in self.empty_values:
    768     raise exceptions.ValidationError(self.error_messages["blank"], code="blank")

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Thanks @ethagnawl and @jerinpetergeorge! Pushed a fix in the commit above.

That was fast, @ankane. Thanks!

Thanks @ethagnawl and @jerinpetergeorge! Pushed a fix in the commit above.

Unfortunately, In my testing, a couple of things were failing.

  • Admin wasn't working at all
  • Updating the data via Django admin wasn't working

These two issues have been taken care of by #22