Reputation: 1361
Suppose I have this model defined in models.py:
[ . . . ]
class Robot(models.Model):
ROBOT_CATEGORIES = (
[ . . . ]
)
name = models.CharField(max_length=30)
version = models.ForeignKey('Version')
category = models.CharField(max_length=10, choices=ROBOT_CATEGORIES)
comments = models.CharField(max_length=2000, blank=True)
def __unicode__(self):
return "ROBOT %s [%s]" % (self.name, self.version)
class Meta:
unique_together = ('name', 'version',)
I want to add Admin functionality to this model. User may enter a comma-separated-list of different Robot names, and save_model
takes care of saving them as different objects. For this, I want to do all validation myself. So here is the Admin Code:
class RobotForm(forms.ModelForm):
name = forms.CharField()
comments = forms.CharField(widget=forms.Textarea, required=False)
def clean_name(self):
# custom validation here
[ . . . ]
return data
def clean(self):
cleaned_data = super(RobotForm, self).clean()
# custom validation here
[ . . . ]
return cleaned_data
class Meta:
model = Robot
class RobotAdmin(admin.ModelAdmin):
form = RobotForm
list_display = ('name','version','category','comments')
def save_model(self, request, obj, form, change):
# custom save method
obj.save()
admin.site.register(Robot, RobotAdmin);
Everything works fine, except for the max_length
limit I set in the model for Robot.name
. In the 'Add' form, if the list's length in the 'Name' field exceeds 30 chars, it flashes an error. I validate each individual name's length in the clean_name
method, and want to disable the max_length validation in the form field. How do I do this?
Upvotes: 1
Views: 2007
Reputation: 19814
In your overridden clean method remove the errors from the cleaned_data like here:
Override data validation on one django form element
Upvotes: 1