1👍
You can make a UniqueConstraint
that works with the lowercase of a name:
class Tags(models.Model):
room = models.ForeignKey(Rooms, related_name='rooms')
room_name = models.CharField(
verbose_name=_('room_name'),
max_length=100,
null=False,
blank=False,
default='',
)
class Meta:
constraints = [
models.UniqueConstrant(
Lower('room_name'), 'room', name='unique_room_name_per_name'
)
]
Converting the string to lowercase is in some cultures however not case-invariant. Indeed, some languages have sophisticated rules regarding when two strings are considered the same, and some characters have no lowercase/uppercase equivalent.
Note: normally a Django model is given a singular name, so
Room
instead of.Rooms
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be:
room
instead of.Room
Note: As the documentation on
unique_together
[Django-doc] says, theunique_together
constraint will likely become deprecated. The documentation advises to use theUniqueConstraint
[Django-doc] from Django’s constraint
framework.