开发者

Django EmailField and full email address with first and last name

开发者 https://www.devze.com 2022-12-24 15:59 出处:网络
I would like to use the EmailField in a form. However, instead of only storing support@acme.com I want to store

I would like to use the EmailField in a form. However, instead of only storing

support@acme.com

I want to store

"ACME Support" <support@acme.com>

The reason is, that when I send email, I would like a "friendly name" to appear. 开发者_JAVA百科Can this be done?


We use Django's email field, and then use a property to render the friendly name in the email.

from django.utils.html import escape
from django.utils.safestring import mark_safe

class MyModel(models.Model):
    email_address = models.EmailField()
    full_name = models.CharField(max_length=30)
    ...

    @property
    def friendly_email(self):
        return mark_safe(u"%s <%s>") % (escape(self.fullname), escape(self.email_address))


Why not store the friendly name in a separate CharField? Alternatively, you could subclass the EmailField and build your own validation. See http://docs.djangoproject.com/en/1.1/howto/custom-model-fields/#howto-custom-model-fields

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号