queryish icon indicating copy to clipboard operation
queryish copied to clipboard

Add __eq__ to VirtualModel

Open alexalligator opened this issue 10 months ago • 0 comments

Hi and thanks for this great library. I'm using it to add Chooser support to Wagtail for Shopify data like Products.

Do you think adding a Django-esque __eq__ method on VirtualModel would be a good idea?

Something like this:

class VirtualModel(metaclass=VirtualModelMetaclass):
    base_query_class = None
    pk_field_name = "id"

    def __eq__(self, other):
        if not isinstance(other, VirtualModel):
            return NotImplemented
        if self._meta.model_name != other._meta.model_name:
            return False
        my_pk = getattr(self, self.pk_field_name, None)
        if my_pk is None:
            return self is other
        return my_pk == getattr(other, other.pk_field_name, None)

Would help unittest queryset results against expected values.

assert ShopifyProduct.objects.get(pk=1) == ShopifyProduct(id="1", handle="product-1", title="Product 1")

alexalligator avatar Feb 14 '25 12:02 alexalligator