我有一个端点来更新公司模型,其中一个字段是“ post_box”,它是一个整数,这是一个可选参数,默认为null。到目前为止,一切都很好。但是,我需要将其设置为null,以便用户可以删除post_box可能具有的任何值。前端发送post_box:如果在我用于此的数字输入字段中为空,则为None。响应为{post_box:“没有一个不是'integer'类型的”“}
我知道“无”不是整数,如何正确执行此操作?
公司模式
class Company(db.Model):
""" Company Model for storing company related details """
__tablename__ = "company"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
public_id = db.Column(db.String(100), unique=True)
company_name = db.Column(db.String(255), unique=True, nullable=False)
organisation_number = db.Column(db.BigInteger, unique=True)
street_name = db.Column(db.String(255), unique=False, nullable=False)
post_code = db.Column(db.Integer, unique=False, nullable=False)
post_area = db.Column(db.String(20), unique=False, nullable=False)
post_box = db.Column(db.Integer, unique=False, nullable=True, default=None)
longitude = db.Column(db.String(30), unique=False, nullable=True, default=None)
latitude = db.Column(db.String(30), unique=False, nullable=True, default=None)
registered_on = db.Column(db.DateTime, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
customers = db.relationship("Customer", cascade="all,delete")
projects = db.relationship("Project", cascade="all,delete")
def __repr__(self):
return "<Name '{}'>".format(self.company_name).encode('utf-8')
保存公司的方法
def save_new_company(data):
company = Company.query.filter_by(organisation_number=data['organisation_number']).first()
if not company:
user = User.query.filter_by(public_id=data['user_public_id']).one()
new_company = Company(
public_id=str(uuid.uuid4()),
company_name=data['company_name'],
organisation_number=data['organisation_number'],
street_name=data['street_name'],
post_code=data['post_code'],
post_area=data['post_area'],
registered_on=datetime.datetime.utcnow(),
user_id=user.id,
)
try:
if data['post_box']:
new_company.post_box = None
except KeyError:
pass
try:
if data['longitude']:
new_company.longitude = data['longitude']
except KeyError:
pass
try:
if data['latitude']:
new_company.latitude = data['latitude']
except KeyError:
pass
save_changes(new_company)
response_object = {
'status': 'success'
}
return response_object, 200
else:
response_object = {
'status': 'fail',
'message': 'Company already exists.',
}
return response_object, 409
控制器相关部分的摘录
@api.expect(_company, validate=True)
@api.response(201, 'Company successfully created.')
@api.doc('create a new company')
def post(self):
"""Creates a new Company """
data = request.json
result = save_new_company(data=data)
return result
还有一些更相关的内容:
class CompanyDto:
api = Namespace('company', description='company related operations')
company = api.model('company', {
'public_id': fields.String(description='company public identifier'),
'company_name': fields.String(required=True, description='Company name'),
'organisation_number': fields.Integer(required=True, description='Organisation number'),
'street_name': fields.String(required=True, description='Company street name'),
'post_code': fields.Integer(required=True, description='Company post code'),
'post_area': fields.String(required=True, description='Company post area'),
'post_box': fields.Integer(required=False, nullable=True, default=None, description='Company post box'),
'longitude': fields.String(required=False, description='Company longitude'),
'latitude': fields.String(required=False, description='Company latitude'),
'user_public_id': fields.String(required=True, description='User identifier'),
})
错误返回:
post_box:“没有一个不是'integer'类型的”