今天首席CTO笔记来给各位分享关于Django怎么获取多个字段的相关内容,其中也会对django计算字段进行详细介绍,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、python django models.Model 遍历所有字段2、请教Django如何获取一个model里字段定义的属性3、django遍历model里面的属性字段4、django 获取 POST 请求值的几种方法5、django怎样获得框架自动定义的自增id字段6、django 多表查询,如何让 select 语句包含多表的所有字段?python django models.Model 遍历所有字段
pcr._meta.get_all_field_names()可以得到所有field的name,然后你可以用pcr._meta.get_field()得到verbose_name,用getattr()得到value
请教Django如何获取一个model里字段定义的属性
你用all() 返回的是一个对象列表。这样的 [obj1, obj2, ...] 使用 obj.objects.get(id=**) 这样得到的是一个对象, 或者使用 get_object_or_404(obj, id=**) 这样的话, 使用 obj.objects.get(id=**).属性名 既可以了 或者 for obj in obj.objec...
django遍历model里面的属性字段
具体的写法是
results = ServerInformation.objects.get(id = 1)#filter是queryset,没有_meta方法
allhost = ServerInformation._meta.get_all_field_names()#这句没错
vername = ServerInformation._meta.get_field('ServerType').verbose_name#这句也没错,S erverType是该模型的一个属性。
vervalue = ServerInformation._meta.get_field('ServerZone').default #即可获取到默认的值,话说你都懂得获取到verbose_name,怎么不会想到直接.default呢。
ps:
getattr(object, name[, default])
Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent to
x.foobar
. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
看看怎么使用。
django 获取 POST 请求值的几种方法
1、django获取post过来的多个键值对:
Ajax:
var languages = {};
languages['english'] = ['mark', 'james'];
languages['spanish'] = ['amy', 'john'];
$.ajax({
type: 'POST',
url: '/save/',
data: languages,
dataType: 'json'
});
Django Views.py
if request.is_ajax() and request.method == 'POST':
for key in request.POST:
print key
valuelist = request.POST.getlist(key)
print valuelist
---------------------
fiddle:
name=june; age=26;
---------------------
views.py
16 for key in request.POST: 17 print key 18 valuelist = request.POST.getlist(key) 19 print valuelist
----------------------------
Development server is running at Quit the server with CONTROL-C. Your method is POST! name [u'june']
age [u'26'] [04/Apr/2012 10:58:11] "POST /getuin/ HTTP/1.1" 200 20
2、一次加载所有值:
def view_example(request):
data=simplejson.loads(request.raw_post_data)
3、获取多个值作为一个列表
request.POST get multiple values
The QueryDict.getlist() allows to get all the checkbox(or select list) values from the request.POST/GET object.
Let’s assume we have a simple form with the following checkboxes. Each checkbox contains an ID of an artist. 1 form method="post" action="" 2 ... 3 input value="1" name="artists" type="checkbox" 4 input value="2" name="artists" type="checkbox" 5 input value="3" name="artists" type="checkbox" 6 ... 7 /form
In views.py : 1 def handle(request): 2 if request.method == 'POST': 3 artists = request.POST.getlist('artists') # now artists is a list of [1,2,3]
django怎样获得框架自动定义的自增id字段
django自定义字段类型,实现非主键字段的自增
# -*- encoding: utf-8 -*-from django.db.models.fields import Field, IntegerFieldfrom django.core import checks, exceptionsfrom django.utils.translation import ugettext_lazy as _class AutoIncreField(Field):
description = _("Integer")
empty_strings_allowed = False
default_error_messages = { 'invalid': _("'%(value)s' value must be an integer."),
} def __init__(self, *args, **kwargs):
kwargs['blank'] = True
super(AutoIncreField, self).__init__(*args, **kwargs) def check(self, **kwargs):
errors = super(AutoIncreField, self).check(**kwargs) # 每张表只能设置一个字段为自增长字段,这个字段可以是主键,也可以不是主键,如果不是主键,则必须设置为一种“键(key)”
# (primary key)也是键(key)的一种,key还包括外键(foreign key)、唯一键(unique key)
errors.extend(self._check_key()) return errors def _check_key(self):
if not self.unique: return [
checks.Error( 'AutoIncreFields must set key(unique=True).',
obj=self,
id='fields.E100',
),
] else: return [] def deconstruct(self):
name, path, args, kwargs = super(AutoIncreField, self).deconstruct() del kwargs['blank']
kwargs['unique'] = True
return name, path, args, kwargs def get_internal_type(self):
return "AutoIncreField"
def to_python(self, value):
if value is None: return value try: return int(value) except (TypeError, ValueError): raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
) def db_type(self, connection):
return 'bigint AUTO_INCREMENT'
def rel_db_type(self, connection):
return IntegerField().db_type(connection=connection) def validate(self, value, model_instance):
pass
def get_db_prep_value(self, value, connection, prepared=False):
if not prepared:
value = self.get_prep_value(value)
value = connection.ops.validate_autopk_value(value) return value def get_prep_value(self, value):
value = super(AutoIncreField, self).get_prep_value(value) if value is None: return None
return int(value) def contribute_to_class(self, cls, name, **kwargs):
assert not cls._meta.auto_field, "A model can't have more than one AutoIncreField."
super(AutoIncreField, self).contribute_to_class(cls, name, **kwargs)
cls._meta.auto_field = self def formfield(self, **kwargs):
return None
django 多表查询,如何让 select 语句包含多表的所有字段?
select * from ( select ....from a group by 班次) as A, (select....from b group by 班次) as B
WHERE A.班次=B.班次
如果是多个字段合为主键,那就用and连起来.
结语:以上就是首席CTO笔记为大家整理的关于Django怎么获取多个字段的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于django计算字段、Django怎么获取多个字段的相关内容别忘了在本站进行查找喔。