Skip to content

Commit c858d0d

Browse files
skirsdedagasman
authored andcommittedAug 8, 2018
Respect serialize=False on ParentalManyToManyFields
·
v6.44.3
1 parent 9168a8f commit c858d0d

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed
 

‎CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changelog
66
* Django 2.1 compatibility
77
* Implemented prefetch_related on FakeQuerySet (Haydn Greatnews)
88
* Fix: Saving a ClusterableModel with a primary key of 0 no longer throws an IntegrityError (A Lee)
9+
* Fix: Serialization now respects `serialize=False` on ParentalManyToManyFields (Tadas Dailyda)
910

1011
4.1 (12.02.2017)
1112
~~~~~~~~~~~~~~~~

‎modelcluster/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ def serializable_data(self):
221221
obj[rel_name] = [get_serializable_data_for_fields(child) for child in children]
222222

223223
for field in get_all_child_m2m_relations(self):
224-
children = getattr(self, field.name).all()
225-
obj[field.name] = [child.pk for child in children]
224+
if field.serialize:
225+
children = getattr(self, field.name).all()
226+
obj[field.name] = [child.pk for child in children]
226227

227228
return obj
228229

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11 on 2018-04-20 10:39
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
import modelcluster.fields
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
dependencies = [
13+
('tests', '0008_prefetch_related_tests'),
14+
]
15+
16+
operations = [
17+
migrations.AddField(
18+
model_name='article',
19+
name='related_articles',
20+
field=modelcluster.fields.ParentalManyToManyField(blank=True, related_name='_article_related_articles_+', serialize=False, to='tests.Article'),
21+
),
22+
migrations.AddField(
23+
model_name='article',
24+
name='view_count',
25+
field=models.IntegerField(blank=True, null=True, serialize=False),
26+
),
27+
]

‎tests/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ class Article(ClusterableModel):
172172
authors = ParentalManyToManyField('Author', related_name='articles_by_author')
173173
categories = ParentalManyToManyField('Category', related_name='articles_by_category')
174174
tags = ClusterTaggableManager(through=TaggedArticle, blank=True)
175+
related_articles = ParentalManyToManyField('self', serialize=False, blank=True)
176+
view_count = models.IntegerField(null=True, blank=True, serialize=False)
175177

176178
def __str__(self):
177179
return self.title

‎tests/tests/test_serialize.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,33 @@ def test_serialise_saves_file_fields(self):
239239
new_doc = Document.from_json(doc_json)
240240

241241
self.assertEqual(new_doc.file.read(), b'Hello world')
242+
243+
def test_ignored_relations(self):
244+
george_orwell = Author.objects.create(name='George Orwell')
245+
charles_dickens = Author.objects.create(name='Charles Dickens')
246+
247+
rel_article = Article(
248+
title='Round and round wherever',
249+
authors=[george_orwell],
250+
)
251+
article = Article(
252+
title='Down and Out in Paris and London',
253+
authors=[george_orwell, charles_dickens],
254+
related_articles=[rel_article],
255+
view_count=123
256+
)
257+
258+
article_serialised = article.serializable_data()
259+
# check that related_articles and view_count are not serialized (marked with serialize=False)
260+
self.assertNotIn('related_articles', article_serialised)
261+
self.assertNotIn('view_count', article_serialised)
262+
263+
rel_article.save()
264+
article.save()
265+
266+
article_json = article.to_json()
267+
restored_article = Article.from_json(article_json)
268+
restored_article.save()
269+
restored_article = Article.objects.get(pk=restored_article.pk)
270+
# check that related_articles and view_count hasn't been touched
271+
self.assertIn(rel_article, restored_article.related_articles.all())

0 commit comments

Comments
 (0)
Please sign in to comment.