python 变量 fields_django.db.models.fields.field.name参数的用途

@total_ordering

class Field(RegisterLookupMixin): # here we have it

... âââââââââ

def __init__(self, verbose_name=None, name=None, primary_key=False,

max_length=None, unique=False, blank=False, null=False,

db_index=False, rel=None, default=NOT_PROVIDED, editable=True,

serialize=True, unique_for_date=None, unique_for_month=None,

unique_for_year=None, choices=None, help_text='', db_column=None,

db_tablespace=None, auto_created=False, validators=(),

error_messages=None):

...

# A guide to Field parameters:

#

# * name: The name of the field specified in the model.

# * attname: The attribute to use on the model object. This is the same as

# "name", except in the case of ForeignKeys, where "_id" is

# appended.

# * db_column: The db_column specified in the model (or None).

# * column: The database column for this field. This is the same as

# "attname", except if db_column is specified.

#

# Code that introspects values, or does other dynamic things, should use

# attname. For example, this gets the primary key value of object "obj":

#

# getattr(obj, opts.pk.attname)

所以如果我们从整体上看

django.db.models.fields.Field

班级,这似乎

name

选项正在设置属性名,这使变量的实名无效:

假设我们有我们的模型:

# models.py

from django.db import models

class SomeModel(models.Model):

first = models.CharField(max_length=50, verbose_name='first', name='second')

third = models.CharField(max_length=50, verbose_name='third')

什么

django-admin shell

告诉我们:

In[2]: from app.models import SomeModel

In[3]: SomeModel.objects.create(first='first', third='third')

Traceback (most recent call last):

File "/Users/ailove/Home/personal/untitled/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code

exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in

SomeModel.objects.create(first='first', third='third')

File "/Users/ailove/Home/personal/untitled/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method

return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/ailove/Home/personal/untitled/venv/lib/python3.6/site-packages/django/db/models/query.py", line 415, in create

obj = self.model(**kwargs)

File "/Users/ailove/Home/personal/untitled/venv/lib/python3.6/site-packages/django/db/models/base.py", line 495, in __init__

raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg)

TypeError: 'first' is an invalid keyword argument for this function

In[4]: obj = SomeModel.objects.create(second='second', third='third')

In[5] obj.third

Out[5]: 'third'

In[6]: obj.second

Out[6]: 'second'

In[7]: obj.first

Traceback (most recent call last):

File "/Users/ailove/Home/personal/untitled/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code

exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in

obj.first

AttributeError: 'SomeModel' object has no attribute 'first'

问题有点宽泛,但我也很好奇。

这是

名称

选择权只是一种有助于发展的东西。

django

或者普通的开发者也可以利用它?如果可以,为什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值