尝试执行GET请求以退回所有已接种疫苗的母牛,但接种疫苗始终返回null。我已经设置了要链接的疫苗和母牛的数据库模型。我不知道怎么了。
Git仓库在这里重现该问题:https://gitlab.com/iandjx/bokujo-api
VaccineModel
{
"Serilog": {
...
"Enrich": [
"FromLogContext",
"WithExceptionDetails"
],
...
}
}
CowModel
from db import db
from datetime import datetime
class VaccineModel(db.Model):
__tablename__ = "vaccines"
id = db.Column(db.Integer, primary_key=True)
vaccine_name = db.Column(db.String(20))
date_given = db.Column(db.DateTime)
cow_id = db.Column(db.Integer, db.ForeignKey('cow.id'))
cow = db.relationship('CowModel')
def __init__(self, vaccine_name, cow_id, date_given=None):
self.vaccine_name = vaccine_name
if date_given is None:
date_given = datetime.now()
self.date_given = date_given
self.cow_id = cow_id
def __repr__(self):
return 'vaccine name : {}, date_given : {}, private_id[] '.format(self.vaccine_name, self.date_given, self.cow_id)
def json(self):
return {'vaccine_name': self.vaccine_name, 'date_given': self.date_given}
序列化器
from db import db
class CowModel(db.Model):
__tablename__ = "cow"
id = db.Column(db.Integer, primary_key=True)
pub_id = db.Column(db.String(20))
private_id = db.Column(db.String(10))
heredity = db.Column(db.String(10))
vaccines_given = db.relationship('VaccineModel', lazy='dynamic')
def __init__(self, pub_id, private_id, heredity):
self.pub_id = pub_id
self.private_id = private_id
self.heredity = heredity
def __repr__(self):
return 'public id : {}, private id : {}'.format(self.pub_id,
self.private_id)
def json(self):
return {'pub_id': self.pub_id, 'private_id': self.private_id}
@classmethod
def find_by_private_id(cls, private_id):
return cls.query.filter_by(id=private_id).first()
cow.py
from flask_restplus import fields
from api.restplus import api
vaccine = api.model('Vaccine', {
'vaccine_name': fields.String(readOnly=True, description='Vaccine Name'),
'date_given': fields.DateTime,
'cow_id': fields.Integer,
})
cow = api.model('Cow Make', {
'pub_id': fields.String(readOnly=True, description='Government ID'),
'private_id': fields.String(required=True, description='Bokujo ID'),
'heredity': fields.String(required=True, description='Heredity of Cow'),
})
cow_with_vaccine = api.inherit('Cow with vaccinations', cow, {
'vaccinations': fields.List(fields.Nested(vaccine))
})
vaccine.py
from flask_restplus import Resource
from api.restplus import api
from api.core.serializers import cow, cow_with_vaccine
from flask import request
from api.core.business import create_cow, find_cow
from api.core.parsers import heredity_arguments
ns = api.namespace('cows', description='Cow Operations')
@ns.route('/')
class Cow(Resource):
'''Shows all cows in he farm'''
@ns.doc('list_cows')
@api.expect(heredity_arguments)
@ns.marshal_list_with(cow_with_vaccine)
def get(self):
'''List all cows'''
args = heredity_arguments.parse_args(request)
return find_cow(args)
@ns.doc('create_cow')
@ns.expect(cow)
@ns.marshal_with(cow, code=201)
def post(self):
'''Create a new cow'''
create_cow(request.json)
return request.json, 201
business.py
from flask_restplus import Resource
from api.restplus import api
from api.core.serializers import vaccine
from flask import request
from api.core.business import find_vaccine,give_vaccine
ns = api.namespace('vaccines', description='Cow Operations')
@ns.route('/')
class Vaccine(Resource):
'''Shows all vaccinen'''
@ns.doc('list_vaccine')
@ns.marshal_list_with(vaccine)
def get(self):
'''List all vaccines given'''
return find_vaccine()
@ns.doc('give_vaccine')
@ns.expect(vaccine)
@ns.marshal_with(vaccine, code=201)
def post(self):
'''Create a new cow'''
give_vaccine(request.json)
return request.json, 201