#33335: Django admin error when adding new object, when using Django 4
functional
unique constraints on model
-----------------------------------------+------------------------
Reporter: Hervé Le Roy | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
When creating a new object from Django Administration site, I'm getting an
error "Tag with this already exists.". (Tag is my model name)
I'm using on this model the new functional unique constraints introducted
in Django 4.0
(https://docs.djangoproject.com/en/dev/ref/models/constraints/#django.db.models.UniqueConstraint).
I suppose this is related to the contraints put on the model. However,
creating a Tag object with code - e.g. `Tag.objects.create(...)` - works
fine. It only fails in Django admin. I'm not sure if it's a bug or
misunderstanding/misconfiguration on my side?
This is my model:
{{{
class Tag(models.Model):
"""Basic tag model."""
# When using get_or_create, we need to pass tag name as a default
value, like this:
# Tag.objects.get_or_create(defaults={'name': tag}, name__iexact=tag,
user=request.user)
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, verbose_name=_('user'))
name = models.CharField(max_length=50, db_index=False,
verbose_name=_('name'),
blank=False, # It's the default behavior, but
let's be explicit
validators=[RegexValidator(regex=r'[A-z0-9À-ž\s]+',
message='This field
accepts only letters, digits and space.')])
slug = models.SlugField(max_length=50, verbose_name=_('slug'))
class Meta:
ordering = ['name']
# Make sure tag name:
# - are unique per user
# - are case insensitive (prevent adding "test" if "Test" already
exists)
# - aren't empty
constraints = [models.UniqueConstraint(fields=['user', 'name'],
name='%(app_label)s_%(class)s_name_unique_per_user'),
models.UniqueConstraint(Lower('name'),
name='%(app_label)s_%(class)s_name_case_insensitive'),
models.CheckConstraint(check=models.Q(name__length__gt=0),
name='%(app_label)s_%(class)s_name_not_empty')]
(snip)
}}}
This is the admin configuration:
{{{
@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = ('pk', 'user', 'name', 'slug')
list_display_links = ['pk']
fields = ('user', 'name',)
list_filter = ('user__email',)
search_fields = ('name',)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33335>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/049.7dd6e7c1cbe6020802e9f9b044bd122b%40djangoproject.com.