Django Admin Form clean method override only working for one exception
Hello, I am trying to override the Forms in the Django Admin interface to have custom exceptions in the clean() method. This works for my first validation, but it doesn't work for a second one in the same form, or on another form at all. I am wondering if I have the syntax wrong for multiple validations. The code looks like this: class ProviderForm(forms.ModelForm): class Meta: model = Provider fields = '__all__' def clean(self): #provider start date and end date provider_start_date = self.cleaned_data.get('provider_start_date') provider_end_date = self.cleaned_data.get('provider_end_date') if provider_start_date > provider_end_date: raise forms.ValidationError("Start date can't be after end date") #etqe_id and provider_code provider_etqe_id = self.cleaned_data.get('provider_etqe_id') provider_code = self.cleaned_data.get('provider_code') if "HW" not in provider_code: raise forms.ValidationError("Invalid provider code") if "9" not in provider_etqe_id: raise forms.ValidationError("Invalid ETQE ID") return self.cleaned_data class ProviderAdmin(admin.ModelAdmin): form = ProviderForm admin.site.register(Provider, ProviderAdmin) (The first validation, the start date and end date, does work perfectly - the second validation does not work at all, and allows the form to be saved no matter what data is entered) Then I also have replicated this for another form: class PersonForm(forms.ModelForm): class Meta: model = Person fields = '__all__' def clean(self): person_alternate_id = self.cleaned_data.get('person_alternate_id') alternate_id_type_id = self.cleaned_data.get('alternate_id_type_id') if person_alternate_id is not None and alternate_id_type_id == 533: raise forms.ValidationError("Person Alternate ID is not blank, therefore Alternate ID Type ID cannot be 533") if person_alternate_id is None and alternate_id_type_id != 533: raise forms.ValidationError("Person Alternate ID is blank, therefore Alternate ID Type ID must be 533") return self.cleaned_data class PersonAdmin(admin.ModelAdmin): form = PersonForm admin.site.register(Person, PersonAdmin) This does not work at all, and also allows the form to be saved even if the invalid data has been entered. Thanks in advance! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6d1e21bc-cc16-42bd-a9b9-949a800c81b7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django Admin Form clean method override only working for one exception
Thanks Julio! This worked. On Thursday, January 4, 2018 at 2:59:16 PM UTC+2, Julio Biason wrote: > > Hi Dean, > > You forgot to call the form clean method (the first line after your `def > clean` should be `super().clean()` as shown in the documentation: > https://docs.djangoproject.com/en/2.0/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other > ). > > On Thu, Jan 4, 2018 at 7:06 AM, dean raemaekers > wrote: > >> Hello, >> >> I am trying to override the Forms in the Django Admin interface to have >> custom exceptions in the clean() method. >> >> This works for my first validation, but it doesn't work for a second one >> in the same form, or on another form at all. I am wondering if I have the >> syntax wrong for multiple validations. >> >> The code looks like this: >> >> class ProviderForm(forms.ModelForm): >> class Meta: >> model = Provider >> fields = '__all__' >> >> def clean(self): >> #provider start date and end date >> provider_start_date = self.cleaned_data.get('provider_start_date') >> provider_end_date = self.cleaned_data.get('provider_end_date') >> if provider_start_date > provider_end_date: >> raise forms.ValidationError("Start date can't be after end date") >> >> #etqe_id and provider_code >> provider_etqe_id = self.cleaned_data.get('provider_etqe_id') >> provider_code = self.cleaned_data.get('provider_code') >> if "HW" not in provider_code: >> raise forms.ValidationError("Invalid provider code") >> >> if "9" not in provider_etqe_id: >> raise forms.ValidationError("Invalid ETQE ID") >> >> return self.cleaned_data >> >> class ProviderAdmin(admin.ModelAdmin): >> form = ProviderForm >> >> admin.site.register(Provider, ProviderAdmin) >> >> (The first validation, the start date and end date, does work perfectly - >> the second validation does not work at all, and allows the form to be saved >> no matter what data is entered) >> >> Then I also have replicated this for another form: >> >> class PersonForm(forms.ModelForm): >> class Meta: >> model = Person >> fields = '__all__' >> >> def clean(self): >> >> person_alternate_id = self.cleaned_data.get('person_alternate_id') >> alternate_id_type_id = self.cleaned_data.get('alternate_id_type_id') >> >> if person_alternate_id is not None and alternate_id_type_id == 533: >> raise forms.ValidationError("Person Alternate ID is not blank, therefore >> Alternate ID Type ID cannot be 533") >> if person_alternate_id is None and alternate_id_type_id != 533: >> raise forms.ValidationError("Person Alternate ID is blank, therefore >> Alternate ID Type ID must be 533") >> >> return self.cleaned_data >> >> class PersonAdmin(admin.ModelAdmin): >> form = PersonForm >> >> admin.site.register(Person, PersonAdmin) >> >> This does not work at all, and also allows the form to be saved even if >> the invalid data has been entered. >> >> Thanks in advance! >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com . >> To post to this group, send email to django...@googlegroups.com >> . >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/6d1e21bc-cc16-42bd-a9b9-949a800c81b7%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/6d1e21bc-cc16-42bd-a9b9-949a800c81b7%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > *Julio Biason*, Sofware Engineer > *AZION* | Deliver. Accelerate. Protect. > Office: +55 51 3083 8101 | Mobile: +55 51 *99907 0554* > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4cf452a6-1232-43d6-9ae0-1ddd310db98e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Django admin panel records disappearing
Hello, I am using the Django admin tool as an internal tool to reach into a remote database. Most of my models work perfectly with the save. method additions I have added, but there are 2 that are not working. What happens is that the admin screen gives a successful "save" message, but the grid view doesn't show any records. When I click on the entry in the right hand "Recent changes" list, it says: Change learner batch back to draft with ID "None" doesn't exist. Perhaps it was deleted? Any advice on why this is happening would be welcome :) Here is the model in question: class ChangeLearnerBatchBackToDraft(models.Model): ticket_number = models.CharField(max_length=10, default='') provider_accreditation_num = models.CharField(max_length=30) batch_id = models.CharField(max_length=30) notes_log = models.TextField(default="No notes added", editable=False) def save(self, *args, **kwargs): try: commando_conn = psycopg2.connect(f"dbname='{DB}' user='{USER}' host='{HOST}' password='{PASS}'") commando_cur = commando_conn.cursor() commando_cur.execute(f"select * from res_partner where provider_accreditation_num='{self.provider_accreditation_num}';") res = commando_cur.fetchone() real_provider_id = res[0] self.notes_log = self.notes_log+f"\n{real_provider_id}\n" except Exception as e: raise AssertionError(e) def __str__(self): return str(self.provider_accreditation_num)+" - "+str(self.batch_id) -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/29e2a257-2631-4075-9453-8ce95b068f8d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
python manage.py inspectdb > models.py crashing with KeyError: options['no-color']
Hello, I am trying to use inspectdb to generate models for a legacy database. I get the following error: $ python custom_inspectdb.py Traceback (most recent call last): File "custom_inspectdb.py", line 8, in Command().execute(table_name_filter=lambda table_name: table_name in tables, database='default') File "/venv/leg370/lib/python3.7/site-packages/django/core/management/base.py", line 314, in execute if options['no_color']: KeyError: 'no_color' -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38702e56-66d6-439b-97de-c47ef36b37e0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.