Hello, the subject expresses my discomfort with certain python 
characteristics, given my background, and my lack of python knowledge.
Specifically lets say that I have a model with a Text field and char field. 
The char field is length 1 and says "use or do not use the text field". The 
char field can have Y or N values.
So using the admin interface I wanted to override the clean method but I 
did not want to write
if text.__len__==0 and char=='Y':
  raise exception

In C/C++ you would use enum for these sort of things. So I ended with 
defining in models.py something as:
TO_USE= (
    ('Y', 'Yes'),
    ('N', 'No'),
    )

class X(models.Model):
    txt= models.CharField(db_index=True,null=True, blank=True,max_length=30)
    use_txt= 
models.CharField(blank=False,max_length=1,default='D',choices=TO_USE)

and in admin.py something as
class XForm(forms.ModelForm):
    def clean(self):
        cleaned_data=super(XForm, self).clean()
        txt= cleaned_data['txt'].strip()
        use_txt=cleaned_data['use_txt'].strip()

        if txt.__len__()==0 and use_txt==TO_USE.__getitem__(0)[0]:
            raise forms.ValidationError('This is needed!')

        return cleaned_data

The part .__getitem__(0)[0] is not very readable. I have looked for enums 
in python, and if I have understood well, it seems they are not implemented.
What is the best way to do it in python for my problem, given that I do not 
want to write =='Y'.
Thanks 
Nenad

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/oqGo6Td_lYoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to