0

Basically, I want to add(patch in) a new Field Option as in here https://docs.djangoproject.com/en/1.10/ref/models/fields/#field-options to the base Field class, so it applies to all other fields

An optional parameter called 'js_inputmask' that is just a string that will be used by a javascript library, for example :

class MyModel(models.Model):
    phone = models.IntegerField(verbose_name='Phone Number', js_inputmask='(99) 9999-9999')

How can I add such? I've heard of monkey patch but not sure how to do it in constructors

Mojimi
  • 2,561
  • 9
  • 52
  • 116
  • Maybe have a look at django's source on github. `EmailField` is a good simple example to start with: https://github.com/django/django/blob/master/django/forms/fields.py#L517 – fips Mar 14 '17 at 19:03
  • @fips I'm not using forms nor do I plan to – Mojimi Mar 14 '17 at 19:05
  • 1
    Have you checked [this](http://stackoverflow.com/questions/13984083/django-extra-argument-for-model-field-definition) ? – nik_m Mar 14 '17 at 19:14
  • BTW, it would be better to use special field to store phone numbers :) https://github.com/stefanfoulis/django-phonenumber-field – Arthur Galuza Mar 14 '17 at 21:04
  • @gaart I guess it's a matter of client vs server validation, the way I see it client seems more intuitive – Mojimi Mar 14 '17 at 21:15
  • 1
    @Mojimi Frontend validation is a first line of defense, it doesn't exclude backend validation and vice versa. Masked field is great, but why not to maximize restrictions and make sure there's no way to put invalid data into DB? – Arthur Galuza Mar 14 '17 at 21:26

0 Answers0