StackOverflow: 
https://stackoverflow.com/questions/62206149/validation-always-true-blank-false-for-charfield-but-not-with-datefield

I have an inlineformset_factory containing a field. When this field is a 
CharField (blank=False) and I run is_valid() on the formset, True is always 
returned even though the CharField is left blank. When I replace the 
CharField with a DateField (default=timezone.now(), blank=False), 
is_valid() will return True only when the field is filled as expected. Why 
is the CharField always returning True when running is_valid() on the 
formset but the DateField does not? Note that I want the CharField to 
behave like the DateField.

Interestingly enough, the formset behaves as expected when both the 
CharField and DateField are present.

Code below is shown with title and date but I have tried with *only* title 
and *only *date as described above. I believe there is a bug causing this 
issue.

models.py

    class Author(models.Model):
        author = models.CharField(max_length=128)
        description = models.CharField(max_length=1000)    
    
    class Book(models.Model):
        author = models.ForeignKey(Author, on_delete=models.CASCADE)
        title = models.CharField(blank=False)
        date = models.DateField(default=timezone.now(), blank=False)


forms.py

    class AuthorForm(forms.ModelForm):
        class Meta:
            model = Author
            fields = ('author', 'description')    
    
    class BookForm(forms.ModelForm):
        class Meta:
            model = Book
            fields = ('title', 'date')    
    
    BookFormSet = forms.inlineformset_factory(
        Author,
        Book,
        form=BookForm,
        fields=('title', 'date'),
        extra=1,
        can_delete=False,
        can_order=False
    )


views.py

    class CreateAuthorView(CreateView):
        template_name = "author_create.html"
        model = Author
        form_class = AuthorForm
    
        def get_context_data(self, **kwargs):
            context = super(CreateAuthorView, self).get_context_data(**
kwargs)
            if self.request.POST:
                form = self.get_form(self.form_class)
                context["book"] = BookFormSet(self.request.POST, instance=
form.instance)
            else:
                context["book"] = BookFormSet()
            return context
    
        def form_valid(self, form):
            context = self.get_context_data()
            book = context["book"]
    
            print(form.data.get("author"))
            print("book.is_valid()", book.is_valid()) # ***This always 
prints True when only CharField is part of Book model***
            
            return redirect(self.get_success_url())
    
        def get_success_url(self):
            return reverse("author_list")


author_create.html

    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <div>
            {{ form }}
        </div>
        <div>
            {{ book.management_form }}
            {% for book_form in book %}
                <div>
                    {{ book_form }}
                </div>
            {% endfor %}
        </div>
        <button type="submit">Submit Author</button>
    </form>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d36c6de5-c04a-41e0-946f-3faa2aa23c5bo%40googlegroups.com.

Reply via email to