I have a model that contains an integer field
class myModel(models.Model): number = models.IntegerField()
Whenever I display or input data to this model I want to do so in octal. When I populate my edit form I do this:
number = oct(numberObject.number).replace('Oo','')
When I go to the edit form it prepopulates with exactly what I want but when I try to do error checking after submitting the form to avoid having that number occur twice I get a problem because number is in self.changed_data and therefore I'm getting the error that this number already exists (basically it's finding itself and saying it is a duplicate)
I can't think of a way to figure out whether I'm trying to change the number to one that already exists or if I'm just submitting the number without changing it.
My form/validation code:
class NumberForm(ModelForm): number = forms.CharField(max_length = 10) def clean_number(self): """Ensures the new Number is unique """ enteredNumber = self.cleaned_data['number'] changedFields = self.changed_data if number.objects.filter(number__exact = int(enteredNumber,8)): if 'number' in changedFields: raise forms.ValidationError("Error") return int(enteredNumber,8) class Meta: model = Number fields = '__all__'