views:

8

answers:

1

I have a form for managing users, a ModelForm type. But for two fields I need a RegexField to validate my data. Because of that I needed to override those fields. But now when I open the form in edit mode (when sending instance of my user) I get no data loaded in both fields. How to avoid it ? Should I forget about regex field and create a custem clean method for them ?

class UserForm(forms.ModelForm):        
    pid = forms.RegexField(regex=r'^\d{11}', max_length=11 ,widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), error_messages=my_default_errors)
    code = forms.RegexField(regex=r'^\d{2}[-]\d{3}', max_length=6, widget=forms.TextInput(attrs=attrs_dict), label="Postal code", error_messages=my_default_errors)

    class Meta:
        model = UserProfile
        exclude = ( 'user', 'image', 'pid' , 'code')

model :

class UserProfile(models.Model):
    def upload_path(self, field_attname):
        filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
        return settings.MEDIA_ROOT + "/uploads/users/%s" % (filename,)
    first_name = models.CharField("Name", max_length=50, blank=True, null=True)
    last_name = models.CharField("Last name", max_length=50, blank=True, null=True)
    pid = models.CharField("PESEL", max_length=11, blank=True, null=True)
    street = models.CharField("Street", max_length=50, blank=True, null=True)
    number = models.CharField("Flat/house number", max_length=10, blank=True, null=True)
    code = models.CharField("Zip ", max_length=6, blank=True, null=True)
    city = models.CharField("City", max_length=50, blank=True, null=True)       
    user = models.ForeignKey(User, unique=True, related_name='profile')
    image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)
A: 

You set the new field type, which is correct, but then you exclude them from being handled for the model, which is not. Remove them from Meta.exclude.

Ignacio Vazquez-Abrams
and should Meta be declared before those fields, or does it make no difference ?
muntu
It doesn't matter. It's all sent to the metaclass in one big lump anyways.
Ignacio Vazquez-Abrams