Django 表单编码风格指南
众所周知,Django 作者提供了 编码风格指南,在编写 Django 应用程序时用作指导。然而,所提供的文件没有具体提及有关表格的内容。
是否已经有一个通用的模式用于此?最好遵循模型风格指南并进行一些小的调整吗?下面是我目前定义表单的方式,并通过随机选择的流行 Django 应用程序,我没有看到与我的方法一致的一致性。使用Django多年的人现在可以提供推荐吗?
class ExampleForm(BetterModelForm):
# start with field definitions, as with Django Model style guide
name = forms.CharField(label="Your Name")
town = forms.CharField(label="Ideal city?")
class Meta:
# all Meta class definitions, as with Django Model style guide
model = models.Example
fields = ['name', 'town', 'example_field']
def save(self):
# ...
def clean_name(self):
# ...
def clean_town(self):
# ...
def clean(self):
# ...
def custom_function(self):
# ...
所以,我的风格是从字段开始,然后是 Meta 类(定义前有一条清晰的线),然后是 save 方法,然后是任何干净的方法,然后是任何自定义方法。如果存在这样的事情,这是否接近标准的推荐方法?
As we know, the Django authors provide a coding style guide, to use as guidance when writing Django applications. However, the document provided mentions nothing specifically about forms.
Is there already a common pattern in use for this? Is the best to follow the Model style guide, with some minor adaptions? Below is how I define forms at the moment, and looking through a random selection of popular Django apps, I fail to see much consistency that agrees with my method. Can those using Django for many years now provide a recommendation?
class ExampleForm(BetterModelForm):
# start with field definitions, as with Django Model style guide
name = forms.CharField(label="Your Name")
town = forms.CharField(label="Ideal city?")
class Meta:
# all Meta class definitions, as with Django Model style guide
model = models.Example
fields = ['name', 'town', 'example_field']
def save(self):
# ...
def clean_name(self):
# ...
def clean_town(self):
# ...
def clean(self):
# ...
def custom_function(self):
# ...
So, my style is to start with the fields, then the Meta class (with a clear line before the definition), then the save method, then any clean methods, and then any custom methods. Is this close to the standard, recommended way of doing it, if such a thing exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
许多 django 的模型风格指南都源自 python 的 pep8 指南,所以如果您还没做过,去那里看看吧!表单没有任何具体指南,但确保应用 模型的规则如果可能的话也形成形式(例如内部元类的顺序)。
但这里最重要的一点可能是保持整个项目的一致性,例如。其他开发人员不会错过长表单类中的某个方法定义,如果他们在某个时刻期望它......但我想每个人都会同意表单类往往会变得非常复杂,并且没有任何官方的指导方针!
Many of django's model style guide lines derive from python's pep8 guidelines, so if you haven't done it yet have a look there! There aren't any specific guidelines for forms, but it's a good practice for sure to apply the rules for models to forms as well if possible (eg. order of the inner meta class).
But probably the most important point here is to keep some consistency over your whole project, so that eg. other developers wouldn't miss a certain method definition in long form class if they are expecting it at a certain point... But I guess everybody will agree that form classes tend to get very complex and there aren't any official guildelines for them!