Skip to content

Commit

Permalink
Respect serialize=False on ParentalManyToManyFields
Browse files Browse the repository at this point in the history
  • Loading branch information
skirsdeda authored and gasman committed Aug 8, 2018
1 parent 9168a8f commit c858d0d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog
* Django 2.1 compatibility
* Implemented prefetch_related on FakeQuerySet (Haydn Greatnews)
* Fix: Saving a ClusterableModel with a primary key of 0 no longer throws an IntegrityError (A Lee)
* Fix: Serialization now respects `serialize=False` on ParentalManyToManyFields (Tadas Dailyda)

4.1 (12.02.2017)
~~~~~~~~~~~~~~~~
Expand Down
5 changes: 3 additions & 2 deletions modelcluster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ def serializable_data(self):
obj[rel_name] = [get_serializable_data_for_fields(child) for child in children]

for field in get_all_child_m2m_relations(self):
children = getattr(self, field.name).all()
obj[field.name] = [child.pk for child in children]
if field.serialize:
children = getattr(self, field.name).all()
obj[field.name] = [child.pk for child in children]

return obj

Expand Down
27 changes: 27 additions & 0 deletions tests/migrations/0009_article_related_articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-04-20 10:39
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import modelcluster.fields


class Migration(migrations.Migration):

dependencies = [
('tests', '0008_prefetch_related_tests'),
]

operations = [
migrations.AddField(
model_name='article',
name='related_articles',
field=modelcluster.fields.ParentalManyToManyField(blank=True, related_name='_article_related_articles_+', serialize=False, to='tests.Article'),
),
migrations.AddField(
model_name='article',
name='view_count',
field=models.IntegerField(blank=True, null=True, serialize=False),
),
]
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class Article(ClusterableModel):
authors = ParentalManyToManyField('Author', related_name='articles_by_author')
categories = ParentalManyToManyField('Category', related_name='articles_by_category')
tags = ClusterTaggableManager(through=TaggedArticle, blank=True)
related_articles = ParentalManyToManyField('self', serialize=False, blank=True)
view_count = models.IntegerField(null=True, blank=True, serialize=False)

def __str__(self):
return self.title
Expand Down
30 changes: 30 additions & 0 deletions tests/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,33 @@ def test_serialise_saves_file_fields(self):
new_doc = Document.from_json(doc_json)

self.assertEqual(new_doc.file.read(), b'Hello world')

def test_ignored_relations(self):
george_orwell = Author.objects.create(name='George Orwell')
charles_dickens = Author.objects.create(name='Charles Dickens')

rel_article = Article(
title='Round and round wherever',
authors=[george_orwell],
)
article = Article(
title='Down and Out in Paris and London',
authors=[george_orwell, charles_dickens],
related_articles=[rel_article],
view_count=123
)

article_serialised = article.serializable_data()
# check that related_articles and view_count are not serialized (marked with serialize=False)
self.assertNotIn('related_articles', article_serialised)
self.assertNotIn('view_count', article_serialised)

rel_article.save()
article.save()

article_json = article.to_json()
restored_article = Article.from_json(article_json)
restored_article.save()
restored_article = Article.objects.get(pk=restored_article.pk)
# check that related_articles and view_count hasn't been touched
self.assertIn(rel_article, restored_article.related_articles.all())

0 comments on commit c858d0d

Please sign in to comment.