Calculate multiple modelField from one Formfield
<p>I need extract multiple ModelField value from one FormField. where I should do this? in <code>clean_&lt;field&gt;</code> functions? with <code>cleaned_data</code> mutation? form <code>__init__</code> function? in <code>model.save</code> or <code>form.save</code> function?</p>

<p>model:</p>

<pre><code>def normalize_name(name):
    # some code
    return name

class MyModel(models.Model):
    name           = models.CharField(max_length=250)
    normalize_name = models.CharField(max_length=250, unique=True)
</code></pre>

<p>form:</p>

<pre><code>class MyForm(forms.ModelForm):
    class Meta:
        model  = MyModel
        fields = ('name',) # or normalize_name? or both?
</code></pre>

<p>According to your comment, I would do the stuff in the <code>save()</code> function. </p>

<p><code>__init__(self)</code> is called before entering the data into the form so it can't  do anything with attributes. </p>

<p>Theoretically, <code>clean_name</code> could work (in my opinion), but it should be used for validation.</p>

<p>The <code>save()</code> method is called after the <code>name</code> attribute is validated so you can get the <code>name</code> and do some stuff with it (normalize_name(name))</p>

<pre><code>def save(self, user, *args, **kwargs):
    name = self.cleaned_data['name']
    self.instance.normalized_name = normalize_name(name)
    return super(YourFormClass, self).save(*args, **kwargs)
</code></pre>

<p>If you mean that you generate <code>normalized_name</code> using <code>name</code> and the form is valid when <code>normalized_name</code> meets some conditions, do it inside clean_name(self).</p>

<pre><code>def clean_name(self):
    name = self.cleaned_data['name']
    normalized_name = normalize(name)
    if not validate(normalized_name):
        raise ValidationError("Something is wrong)
    return name
</code></pre>

python django modelform