First of all, my web site uses Pogesql Database.
Some model field in the data model needs to save to database with lowercase letter .
this scenario includes both doing in the admin page and end-user page.
The key point is that I don’t want to customize too much in the code file. just configure a little bit in the file.
as I research, there is nothing much to refer about this thing in link below.
The link below is a likely way to deal with this issue, but i prefer to do with the other way apart from that.
This link shows 2 options for this issue
- With data cleaners in Form.py
class SignUpForm(UserCreationForm): # your code email = forms.EmailField(required=True) def clean_email(self): data = self.cleaned_data['email'] return data.lower()
- With a custom field:
class EmailLowerField(forms.EmailField): def to_python(self, value): return value.lower()
In my thought, there are pros and cons.
- for “With data cleaners” is suitable for user form ( it is not in admin form).
2.for “With a custom field” , I am concerned about running makemigration and migrate command to a database problem. it is probable to fail to migrate to database
Are there other ways to deal with my question?
In this case of lower character, there are brand_name in class Brand along with model_name in class Model
Supposing, I fill data in model form model_name=XXt001 , then it is saved as xxt001 instead.
Below is models.py
class Brand(models.Model): brand_name=models.CharField(max_length=100,unique=True) # prefer lowercase letter def __str__(self): return self.brand_name class Model(models.Model): model_name=models.CharField(max_length=100) # want lowercase letter brand=models.ForeignKey(Brand,on_delete=models.CASCADE) def __str__(self): return self.model_name