Mathias
Mathias

Reputation: 334

How to get a field from the model in a forms.ModelForm

I want to get the value of a form field called 'some_attribute'. I try this with self.Meta.model.some_attribute:

class TestModel(models.Model):
    some_attribute = models.CharField(max_length=255)


class TestForm(forms.ModelForm):

    def save(self, *args, **kwargs):
        super(TestForm, self).save(*args, **kwargs)
        send_mail('topic', self.Meta.model.some_attribute, '[email protected]', ['[email protected]'],
              fail_silently=False)

    class Meta:
        model = TestModel

This results in an error: 'EnrollInEventForm' object has no attribute 'some_attribute'. How do I do that?

Upvotes: 1

Views: 78

Answers (2)

Mickaël Bucas
Mickaël Bucas

Reputation: 691

It may be that some_attribtue is misspelled in Model class, t and u exchanged.

Upvotes: 1

Cole
Cole

Reputation: 2509

I occasional strip a specific value from a form inside a view. I do that by using the form.data dictionary. In your case, one of the keys should be some_attribute. This approach might help.

Upvotes: 1

Related Questions