I have a parent model:
class AbstractAddress(models.Model):
name = models.CharField(max_length=100,verbose_name='Name')
address1 = models.CharField(max_length=100,null=True,blank=True)
city = models.CharField(max_length=100,null=True,blank=True)
phone = models.CharField(max_length=10,null=True,blank=True)
pincode = models.IntegerField(null=True,blank=True, default=0)
class Meta:
abstract = True
This model I am inheriting to create Vendor, Client, Employee models. Now in admin ModelForm:
pincode = CharField(widget=TextInput(attrs={'type':'number'}),
validators=[RegexValidator(regex='^.{6}$',message='Pincode can have 6 digits')],
required=False, min_length=6)
phone = CharField(widget=TextInput(attrs={'type':'number',
'size':'10'}),
validators=[RegexValidator(regex='^.{10}$', message='Phone can have 10 digits')],
required=False, min_length=10)
This code I do not want to write for every modelform for the above mentioned models. How can I use DRY for the same?
I have defined the validation in the project in the same folder as the settings.py and defined the validations methods as:
def validated_pincode():
pincode = CharField(widget=TextInput(attrs={'type':'number'}),
validators=[RegexValidator(regex='^.{6}$',message='Pincode can have 6 digits')],
required=False, min_length=6)
return pincode
in the modeladmin form I call the method:
pincode = validated_pincode()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.