django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

can the instance_of Q-object be made to accept a string?

Open johncronan opened this issue 4 years ago • 2 comments

I have found that, in my base model class, I want to add a UniqueTogether to the constraints in the Meta, with a condition of the form:

~Q(instance_of=DerivedModel)

where DerivedModel is inheriting from this base class. Because of the inheritance, I have to define DerivedModel after this one, but that also means it's not yet defined for me to reference in the Meta class.

Can this Q-object be made to accept a string, as well? That's how other components, such as ForeignKey, deal with the same type of problem with circular dependency.

Or is there a solution here that I'm not thinking of?

johncronan avatar Dec 05 '21 00:12 johncronan

This is probably an issue that needs to be resolved in Django rather than in django-polymorphic

AdamDonna avatar Feb 07 '22 23:02 AdamDonna

The main reason I might want to have it that deep would be to use the constraint for serializing on natural keys.

In the case I'm dealing with, there's another combination of fields that works for that. So this is not a big deal.

But I don't see how Django could resolve it, since instance_of is implemented here. In _get_mro_content_type_ids, we query the Content Type framework with get_for_model. The alternative is get_by_natural_key, which takes app_label and model name:

https://docs.djangoproject.com/en/4.0/ref/contrib/contenttypes/#django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key

The primary purpose of this method is to allow ContentType objects to be referenced via a natural key during deserialization.

johncronan avatar Feb 07 '22 23:02 johncronan