django怎样获得框架自动定义的自增id字段 10
我使用resp=Goods.objects.all()获取的数据里面不包含id初学者不懂,求大神解答我model中没有id是因为框架已经自动定义,但是我想获得它model...
我使用 resp = Goods.objects.all()获取的数据里面不包含id初学者不懂,求大神解答我model中没有id是因为框架已经自动定义,但是我想获得它model定义如图
展开
3个回答
展开全部
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
# -*- 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的表单验证框架可以从数据模型自动生成HTML表单,还可在与数据存储区交互的同时无缝处理通过表单输入的信息。要在Google App Engine上使用Django Forms,参考这篇文章在使用过程中会遇到一个问题,比如你在定义一个数据模型Book
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
good = Goods.objects.all().values()
这个方法可以 获得每个字段的字典
这个方法可以 获得每个字段的字典
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询