5

I want to populate a field before validation in admin.

models.py

class Ad(models.Model):
    .....
    manual_code  = models.BooleanField("Manual Code", default=False)
    code          = models.TextField("Code")

admin.py

class MyAdAdminForm(forms.ModelForm):
    class Meta:
        model = Ad

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_data['code'] = "dadad"
        print cleaned_data
        return cleaned_data

class AdAdmin(admin.ModelAdmin):
    form = MyAdAdminForm

admin.site.register(Ad, AdAdmin)

eventually I wanna generate that whole "code" field, but I'm still getting the error in admin that the field is empty although I can see the value of it ("dadad") in the shell.

I also tried

def clean_code(self):

and it wasn't calling that function at all.

and I also tried

def save_model(request,....):

in the AdAdmin class but it wasn't calling that either.

so what should I do?

dado_eyad
  • 601
  • 1
  • 8
  • 28

1 Answers1

2

As well as calling your custom clean method, Django validates each form field individually. It is the individual field check that is causing the required field error. For full details see the Django docs on Form and field validation.

If you do not require user input in the code field, you can override the form's __init__ method, and set required=False.

class MyAdAdminForm(forms.ModelForm):
    class Meta:
        model = Ad

    def __init__(self, *args, **kwargs):
        super(MyAdAdminForm, self).__init__(*args, **kwargs)
        self.fields['code'].required = False

Once the field is not required, the form will call the clean_code method during validation, so you can set the value there.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I wanted to keep the field required. that was the whole problem. – dado_eyad May 17 '12 at 11:59
  • As I understand it, you are automatically generating the value of the `code` field, so you don't require the user to enter the code. Therefore required should be false for this form. If you do require the user to enter the value, then showing an error when the user hasn't entered a value seems to be the correct behaviour. – Alasdair May 17 '12 at 12:06
  • I might not generate the value of the field if the `manual_code` is checked. so if the user checked it and didn't insert any value for `code` it should return an error to them. – dado_eyad May 17 '12 at 13:03
  • But anyway I'm just gonna go with not required. Thanks. – dado_eyad May 17 '12 at 13:03
  • If it is not *always* required, then setting `required=False` is the right choice. You can raise an error in your `clean` method if the user has checked `manual_code`, but not provided a `code`. See the Django docs for [Cleaning and validating fields that depend on each other](https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other) for a similar example. – Alasdair May 17 '12 at 13:59
  • It's usually better to dump the checkbox condition and simply generate the code if the code field is empty. – Chris Pratt May 17 '12 at 14:58