[英]Understanding Django (from source code): attributes of field via models.CharField and forms.CharField
I would like to understand how django.db.models
and django.forms
work together on a field
. 我想了解
django.db.models
和django.forms
如何在field
上一起工作。
Consider this example: 考虑以下示例:
# models.py
from django.db import models
class Person(models.Model)
Telefon1 = models.CharField('Telefon', max_length=30, blank=True, null=True)
displays via generic.edit.FormView
in views
the form field in HTML correctly with the default value 'Telefon'. 通过
generic.edit.FormView
在views
中正确显示带有默认值“ Telefon”的HTML表单字段。 In order to turn off autocomplete-functionality I add the following: 为了关闭自动完成功能,我添加了以下内容:
# forms.py
from django import forms
class ContactForm(forms.ModelsForm):
Telefon1 = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}))
The field then is correctly rendered with automplete:off
. 然后使用
automplete:off
正确渲染该字段。 But the name of the field falls back to the models
name: Telefon1
但是字段的名称回退到
models
名称: Telefon1
So question 1 : How do I make sure that the default name defined in models
is being passed through or do I have to re-define the name with forms.CharField(widget=...
? 所以问题1 :如何确保通过
models
定义的默认名称,还是必须使用forms.CharField(widget=...
来重新定义名称?
Question 2 : How can I find the answer reading the source code? 问题2 :如何找到阅读源代码的答案? I started by reading
site-packages\\django\\forms\\widget.py
which either doesn't include the answer or I did not understand. 我从阅读
site-packages\\django\\forms\\widget.py
,它不包含答案或我听不懂。
I would like to learn and understand Django via source code since the tutorial and documentation doesn't always work for me (especially in this case). 我想通过源代码学习和理解Django,因为该教程和文档并不总是对我有用(特别是在这种情况下)。
Talking about django.db.models.Field
and django.forms.Field
are exactly two different machines. 谈论
django.db.models.Field
和django.forms.Field
正是两个不同的机器。 But still in real life we need forms that supposedly need to map the models. 但是在现实生活中,我们仍然需要可以映射模型的表格。 To accomplish that, django has provided us with the concept of django modelforms which we make by passing name of the model.
为此,django通过传递模型名称为我们提供了django模型形式的概念。
By default django modelforms shows same name as that of django models but in case you need to override the name, you can by overriding __init()
in django modelforms as also specified in here to further clarify your understanding. 默认情况下,django modelforms显示的名称与django model相同,但是如果需要覆盖名称,可以通过在django modelforms中覆盖
__init()
来实现,以进一步阐明您的理解。
The code base of django is already developed and is still being developed to the extent of optimization. django的代码库已经开发,并且仍在不断优化中。 The point of learning is deeply admired but falling down into complexities is just waste of effectiveness and beats the purpose of being called as a Rapid Application Development Technology.
学习的观点令人钦佩,但陷入复杂性只是浪费效率,并且超出了被称为快速应用程序开发技术的目的。
Whatever is given in official docs is self sufficient and deep enough to help you gain a great understanding. 官方文档中提供的所有内容都足够自我和深度,可以帮助您获得深刻的理解。 Ths only best way to learn a framework is to learn its error messages then you will automatically be the master of intricacies of this framework.
学习框架的唯一最佳方法是学习其错误消息,然后您将自动成为该框架的复杂者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.