Skip to content

Commit

Permalink
Merge pull request #18 from dpwrussell/include_overidden_by_fk_field_…
Browse files Browse the repository at this point in the history
…that_should_be_serialized

Include overidden by fk field that should be serialized
  • Loading branch information
sloria committed Sep 1, 2015
2 parents edba7b1 + 0c115bd commit c1eaacb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions marshmallow_sqlalchemy/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ def fields_for_model(self, model, include_fk=False, fields=None):
if fields and prop.key not in fields:
continue
if hasattr(prop, 'columns'):
if not include_fk and prop.columns[0].foreign_keys:
continue
if not include_fk:
# Only skip a column if there is no overriden column
# which does not have a Foreign Key.
for column in prop.columns:
if not column.foreign_keys:
break
else:
continue
field = self.property2field(prop)
if field:
result[prop.key] = field
Expand Down
42 changes: 42 additions & 0 deletions tests/test_marshmallow_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ class SubstituteTeacher(Base):
id = sa.Column(sa.Integer, sa.ForeignKey('teacher.id'),
primary_key=True)

class Paper(Base):
__tablename__ = 'paper'

satype = sa.Column(sa.String(50))
__mapper_args__ = {
'polymorphic_identity': 'paper',
'polymorphic_on': satype
}

id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String, nullable=False, unique=True)

class GradedPaper(Paper):
__tablename__ = 'gradedpaper'

__mapper_args__ = {
'polymorphic_identity': 'gradedpaper'
}

id = sa.Column(sa.Integer, sa.ForeignKey('paper.id'),
primary_key=True)

marks_available = sa.Column(sa.Integer)

# So that we can access models with dot-notation, e.g. models.Course
class _models(object):
def __init__(self):
Expand All @@ -126,6 +150,8 @@ def __init__(self):
self.Student = Student
self.Teacher = Teacher
self.SubstituteTeacher = SubstituteTeacher
self.Paper = Paper
self.GradedPaper = GradedPaper
return _models()

@pytest.fixture()
Expand Down Expand Up @@ -153,6 +179,15 @@ class Meta:
class SubstituteTeacherSchema(ModelSchema):
class Meta:
model = models.SubstituteTeacher

class PaperSchema(ModelSchema):
class Meta:
model = models.Paper
sqla_session = session

class GradedPaperSchema(ModelSchema):
class Meta:
model = models.GradedPaper
sqla_session = session

class HyperlinkStudentSchema(ModelSchema):
Expand All @@ -168,6 +203,8 @@ def __init__(self):
self.StudentSchema = StudentSchema
self.TeacherSchema = TeacherSchema
self.SubstituteTeacherSchema = SubstituteTeacherSchema
self.PaperSchema = PaperSchema
self.GradedPaperSchema = GradedPaperSchema
self.HyperlinkStudentSchema = HyperlinkStudentSchema
return _schemas()

Expand Down Expand Up @@ -227,6 +264,11 @@ def test_include_fk(self, models):
student_fields2 = fields_for_model(models.Student, include_fk=True)
assert 'current_school_id' in student_fields2

def test_overridden_with_fk(self, models):
graded_paper_fields = fields_for_model(models.GradedPaper,
include_fk=False)
assert 'id' in graded_paper_fields

def make_property(*column_args, **column_kwargs):
return column_property(sa.Column(*column_args, **column_kwargs))

Expand Down

0 comments on commit c1eaacb

Please sign in to comment.