I have original question here How to access the names of fields in Django UniqueConstraint? but it do not answer my question when
*expressions
contains different kind of "fields". I left the accepted answer.
I want to access the unique constraint field names when declared in Meta class :
class Book(models.Model):
name = CharField(...)
author = ForeignKey(...)
description = ...
class Meta:
constraints = [
UniqueConstraint(
Lower("name"), "author",
name="unique__book_author",
),
]
def do(self):
unique_fields = ... # should be ["name", "author"]
All fields are not necessarily a function like Lower
. In this case, author is a foreign key.
See https://docs.djangoproject.com/en/4.1/ref/models/constraints/