[Fixed]-Custom model fields and form fields in reusable Django app: naming convention

1👍

While Django places model fields in django.db.model.fields and form fields in django.forms.fields, for the purposes of most Django developers, this is just an internal implementation detail.

Model and form fields are accessed directly through the models and forms modules:

from django.db import models
from django import forms

class Question(models.Model):
    question_test = models.CharField(max_length=200)

class NameForm(forms.Form):
    your_name = forms.CharField(label='Your name', max_length=100)

For a simple app, could just place model and form place fields in models.py and forms.py, respectively. This way, the interface to your app will match that of Django.

For a more complex app, you can create a hierarchy with db/fields.py and forms/fields.py. But if you’re trying to follow Django’s interface closely, you would want the fields to still be accessible through models or forms modules. To achieve that, you would want to import the contents of the relevant fields.py file into db/models.py or forms/__init__.py with something like:

from .fields import *

Hope this helps.

Leave a comment