error on duplicated lot name
This commit is contained in:
parent
bf0ffc52f2
commit
12b9be4276
22
lot/migrations/0008_lot_unique_institution_and_name.py
Normal file
22
lot/migrations/0008_lot_unique_institution_and_name.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 5.0.6 on 2025-02-21 20:58
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("lot", "0007_lottag_inbox"),
|
||||
("user", "0002_institution_algorithm"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddConstraint(
|
||||
model_name="lot",
|
||||
constraint=models.UniqueConstraint(
|
||||
fields=("owner", "name"), name="unique_institution_and_name"
|
||||
),
|
||||
),
|
||||
]
|
13
lot/views.py
13
lot/views.py
|
@ -39,9 +39,16 @@ class NewLotView(DashboardView, CreateView):
|
|||
return form
|
||||
|
||||
def form_valid(self, form):
|
||||
form.instance.owner = self.request.user.institution
|
||||
form.instance.user = self.request.user
|
||||
response = super().form_valid(form)
|
||||
try:
|
||||
form.instance.owner = self.request.user.institution
|
||||
form.instance.user = self.request.user
|
||||
response = super().form_valid(form)
|
||||
return response
|
||||
|
||||
except IntegrityError:
|
||||
messages.error(self.request, _("Lot name is already defined."))
|
||||
return self.form_invalid(form)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue