Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - TextField cannot be null or blank

This is my model:

class Blog(models.Model):
    title = models.CharField(max_length=300, blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to=get_upload_file_name, blank=True, null=True, default='')
    pubdate = models.DateTimeField(default=timezone.now)
    of_type = models.CharField(max_length=2,choices=TYPES)

When I syncdb, it does syncdb correctly. But when in the admin I try to save a blog without description, it does not. It shows error to

This field is required.

How do I overcome this?

In the admin however I am using ckeditor to use its widget:

class BlogAdminForm(forms.ModelForm):
    description = forms.CharField(widget=CKEditorWidget())
    class Meta:
        model = Blog

class BlogAdmin(admin.ModelAdmin):
    form = BlogAdminForm

admin.site.register(Blog, BlogAdmin)

Am I doing something wrong in the code? Please help me solve this. I will be very much grateful. Thank you.

like image 468
Kakar Avatar asked Feb 11 '23 09:02

Kakar


1 Answers

Add the required=False argument to the form field definition:

description = forms.CharField(required=False, widget=CKEditorWidget())

BTW, you can use the formfield_overrides option of the ModelAdmin instead of replacing the whole form:

class BlogAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': CKEditorWidget()},
    }
like image 61
catavaran Avatar answered Feb 13 '23 22:02

catavaran