Reputation: 10554
I'm writing a reusable Django app that provides both custom model fields and custom form fields. I was thinking to place one of the two pieces in a file called fields.py
but, clearly, I cannot place both the model and form fields in the same file.
My question is: is there any naming convention or recommended best practice for naming custom model fields and custom form fields in a reusable app?
I was thinking of copying the structure of Django, but the fields file are both in separate modules (django.db.model.fields
and django.forms.fields
) while my form and model fields live in the same module (the app).
The documentation says nothing about naming conventions in this case: it simply explains how to code the fields, not where the code should be (that is left to the user).
Upvotes: 0
Views: 107
Reputation: 2678
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.
Upvotes: 1