Django Version: 3.1.5
Python Version: 3.6.9
Model
class GoogleAnalytics(models.Model):
counter = models.SlugField(max_length=17,
null=False,
default="",
unique=True,
verbose_name="Counter")
tracking_code = models.TextField(null=False,
default="",
verbose_name="Tracking code",
validators=[validate_google_analytics_tracking_code])
Form
class CounterForm(forms.ModelForm):
"""
Validate if the value in counter field
really corresponds to the tracking code for this counter.
"""
def clean(self):
counter_manager = CounterManager()
if "GoogleAnalytics" in str(self.Meta.model):
counter_from_tracking = counter_manager.get_counter(self.cleaned_data['tracking_code'],
Counters.GOOGLE_ANALYTICS)
else:
assert "YandexMetrika" in str(self.Meta.model)
counter_from_tracking = counter_manager.get_counter(self.cleaned_data['tracking_code'],
Counters.YANDEX_METRIKA)
if self.cleaned_data['counter'] != counter_from_tracking:
raise ValidationError("Код не соответствует счетчику")
return self.cleaned_data
class Meta:
model = GoogleAnalytics
exclude = []
Admin
class GoogleAnalyticsAdmin(admin.ModelAdmin):
form = CounterForm
admin.site.register(GoogleAnalytics, GoogleAnalyticsAdmin)
Traceback
https://dpaste.com/6CUL2VZAW
Well, unique constraint for counter field worked.
But I extected a validation error rather than IntegrityError. Could you tell me how to fix that?
question from:
https://stackoverflow.com/questions/65869639/integrityerror-wrong-instead-of-validationerror-correct 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…