2

尝试执行 GET 请求以返回所有接种疫苗的奶牛,但疫苗接种总是返回 null。我已经为要链接的疫苗和奶牛设置了数据库模型。我不确定有什么问题。

Git repo 在这里重现问题:https ://gitlab.com/iandjx/bokujo-api

疫苗模型

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()

序列化器

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))
})

牛.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

疫苗.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

业务.py

# from rest_api_demo.database import db
# from rest_api_demo.database.models import Post, Category
from db import db
from models.cow import CowModel
from models.vaccine import VaccineModel


def create_cow(data):
pub_id = data.get('pub_id')
private_id = data.get('private_id')
heredity = data.get('heredity')
cow = CowModel(pub_id, private_id, heredity)
db.session.add(cow)
db.session.commit()


def find_cow(data):
if data.get('heredity') == 'all':
    return CowModel.query.all()
if data.get('heredity'):
    return CowModel.query.filter_by(heredity=data.get('heredity')).all()


def give_vaccine(data):
vaccine_name = data.get('vaccine_name')
# date_given = data.get('date_given')
cow_id = data.get('cow_id')
print(data)
vaccine = VaccineModel(vaccine_name, cow_id)
print(vaccine)
db.session.add(vaccine)
db.session.commit()


def find_vaccine():
return VaccineModel.query.all()
4

0 回答 0