48
Try to overwrite the save()
method of the Model
, to check for empty values:
class MyModel(models.Model):
my_nullable_string = models.CharField(max_length=15, null=True, blank=True)
def save(self, *args, **kwargs):
if not self.my_nullable_string:
self.my_nullable_string = None
super(MyModel, self).save(*args, **kwargs)
20
This section in the docs makes it sound like you can’t set a string-based field to NULL
through the admin; it will use the empty string. This is just the way Django does it. It will work for other types of fields.
You’ll either have to hack on the admin script or else decide it doesn’t really need to be NULL in the database; an empty string is OK.
- [Django]-How to strip html/javascript from text input in django
- [Django]-Including a querystring in a django.core.urlresolvers reverse() call
- [Django]-Django template comparing string
12
try this code, here Language.subregion hass null=True
this code overwrites admin form settings (LanguageAdmin) and sets “subregion” field property – required to False
from app.models import Language
from django.contrib import admin
class Language(models.Model):
subregion = models.ForeignKey(SubRegion, null=True)
code = models.CharField(max_length=10, unique=True)
class LanguageAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(LanguageAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['subregion'].required = False
return form
admin.site.register(Language, LanguageAdmin)
- [Django]-Substring in a django template?
- [Django]-Location of Django logs and errors
- [Django]-Django returning HTTP 301?
11
Usually, when you pass both null=True
and blank=True
, if you leave the field blank in the admin, Django
will use NULL
for its value.
EDIT:
as agf
explains in his answer, this is true for all types except CharField
and TextField
.
- [Django]-A good way to redirect with a POST request?
- [Django]-Django project base template
- [Django]-Django release 1.5: 'url' requires a non-empty first argument. The syntax changed in Django 1.5
5
I often use Django just for the Admin and need to preserve a lot of NULLs in the db. I use this snippet to set all empty strings on a particular object to NULL.
def save(self, *args, **kwargs):
for var in vars(self):
if not var.startswith('_'):
if self.__dict__[var] == '':
self.__dict__[var] = None
super(MyModel, self).save(*args, **kwargs)
- [Django]-Django BooleanField as radio buttons?
- [Django]-How do you filter a nested serializer in Django Rest Framework?
- [Django]-Django-celery: No result backend configured
3
Note that this is fixed in Django 2.0.1 — from that version on, empty values will be saved as None for nullable Charfields.
Ticket: https://code.djangoproject.com/ticket/4136
Commit: https://github.com/django/django/commit/267dc4adddd2882182f71a7f285a06b1d4b15af0
- [Django]-Django – comparing old and new field value before saving
- [Django]-How to access outermost forloop.counter with nested for loops in Django templates?
- [Django]-Django REST framework: non-model serializer