17๐
If you are looking for a text/char field and do not want it to strip white spaces you can set strip=False in the constructor method of a form and then use the form in the admin
class YourForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self.fields['myfield'].strip = False
class Meta:
model = YourModel
fields = "__all__"
You can then use this form in the admin by specifying form=YourForm
in the admin.py file.
13๐
Try using this:
# fields.py
from django.db.models import TextField
class NonStrippingTextField(TextField):
"""A TextField that does not strip whitespace at the beginning/end of
it's value. Might be important for markup/code."""
def formfield(self, **kwargs):
kwargs['strip'] = False
return super(NonStrippingTextField, self).formfield(**kwargs)
And in your model:
class MyModel(models.Model):
# ...
my_field = NonStrippingTextField()
- [Django]-Github issues api 401, why? (django)
- [Django]-How to implement FirebaseDB with a Django Web Application
- [Django]-Django admin TabularInline โ is there a good way of adding a custom html column?
7๐
strip=False
in the model field for CharFields.
Django TextField do not support this stripping feature so you have to do it on your own. You can use the strip method.
abc.strip()
- [Django]-Django composite unique on multiple model fields
- [Django]-Django FileField upload is not working for me
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
5๐
Seems like the best way to handle this is to create a custom admin form like this:
class CustomForm(forms.ModelForm):
my_field = forms.CharField(strip=False, widget=forms.Textarea)
class Meta:
model = MyModel
exclude = []
This will create a default form with just my_field overwritten with its non stripped version. )this has to be set in the corresponding admin of course. If anybody knows an even simpler version. Please tell me!
- [Django]-Celery discover tasks in files with other filenames
- [Django]-How do I use an UpdateView to update a Django Model?
- [Django]-Django: using more than one database with inspectdb?
3๐
You can also achieve this by modifying ModelAdmin
.
Overridding formfield_for_dbfield
function and setting kwargs['strip'] = False
for title
field will disable auto trim for it.
@admin.register(Example)
class ExampleAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, request, **kwargs):
if db_field.name == 'title':
kwargs['strip'] = False
return super().formfield_for_dbfield(db_field, request, **kwargs)
Ref: https://www.aaronoellis.com/articles/allow-whitespace-to-be-a-valid-charfield-value-in-django-admin
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
- [Django]-How do I deploy Django on AWS?
- [Django]-With DEBUG=False, how can I log django exceptions to a log file
2๐
I was having this issue with django-rest model serializer. The data in my text field was stripped of white space. So if you are trying to do this on the serializer level, you can specify the whitespace param on CharField serializer. Here is the source code signature.
And here is the rest-docs on CharField
class SomeSerializer(serializers.ModelSerializer):
content = serializers.CharField(trim_whitespace=False)
class Meta:
model = YourModel
fields = ["content"]
- [Django]-Annotate a queryset with the average date difference? (django)
- [Django]-Is there a way to filter a queryset in the django admin?
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language