· 

Django: fields

1. Intro

This is /mysite/myarts/views.py:

from django.shortcuts import render

# Create your views here.

from myarts.models import Article

from myarts.owner import OwnerListView, OwnerDetailView, OwnerCreateView, OwnerUpdateView, OwnerDeleteView

class ArticleListView(OwnerListView):

    model = Article

    # By convention:

    # template_name = "myarts/article_list.html"

class ArticleDetailView(OwnerDetailView):

    model = Article

class ArticleCreateView(OwnerCreateView):

    model = Article

    # List the fields to copy from the Article model to the Article form

    fields = ['title', 'text']

class ArticleUpdateView(OwnerUpdateView):

    model = Article

    fields = ['title', 'text']

    # This would make more sense

    # fields_exclude = ['owner', 'created_at', 'updated_at']

class ArticleDeleteView(OwnerDeleteView):

    model = Article

 

Some views have the attribute "fields".  I wondered where this "fields" is defined or declared?

 

2. Where is "fields" declared?

I found "fields" in class ModelFormMixin:

 

3. ChatGPT's Comfirmation

I found "fields" in "class ModelFormMixin(FormMixin, SingleObjectMixin)" which is in /django/views/generic/edit.py

ModelFormMixin is a parent of "class BaseCreateView(ModelFormMixin, ProcessFormView)".

BaseCreateView is a parent of "class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView)".

Therefore, "field" is defined in ModelFormMixin.  And Article overrides this "fields". Right? 

 

Yes — you are exactly right! 🎯

You've traced it perfectly through Django's class-based view (CBV) inheritance chain.

Let’s confirm and summarize what you just discovered:


✅ The Inheritance Path

text

CreateView

├── inherits from BaseCreateView

│                                  ├── inherits from ModelFormMixin

│                                                                        └── defines: fields = None

 

So in /django/views/generic/edit.py:

python
class ModelFormMixin(FormMixin, SingleObjectMixin):
fields = None # <== Default value

This is where the fields attribute is declared — but not set.


✅ In Your View

When you write:

python
class ArticleCreateView(OwnerCreateView):
model = Article
fields = ['title', 'text']

You are overriding the default None value of fields from ModelFormMixin and telling Django:

“Use title and text fields from the Article model to build the form.”

So yes — your class overrides the fields attribute inherited from ModelFormMixin.


🔍 Why this matters

If you don’t override fields, Django would raise an error like:

sql
ImproperlyConfigured: ArticleCreateView must define 'fields' or 'form_class'.

Django needs either:

  • fields = [...] list
    or

  • A custom form_class = ArticleForm


✅ Summary

Class Role
ModelFormMixin Defines fields = None
BaseCreateView Inherits ModelFormMixin
CreateView Inherits BaseCreateView
ArticleCreateView Overrides fields to build a form

 

You understood the chain perfectly. Let me know if you’d like to explore form_class next — it’s the more flexible sibling of fields.