Skip to content

Commit

Permalink
Add _result_cache property on FakeQuerySet
Browse files Browse the repository at this point in the history
This is necessary for model forms with ParentalManyToManyFields to work correctly on Django 1.8-1.9
  • Loading branch information
gasman committed Feb 2, 2017
1 parent f1fe347 commit 5c5ebdc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

3.0.1 (02.02.2017)
~~~~~~~~~~~~~~~~~~
* Fix: Added _result_cache property on FakeQuerySet (necessary for model forms with ParentalManyToManyFields to work correctly on Django 1.8-1.9)

3.0 (02.02.2017)
~~~~~~~~~~~~~~~~
* Added support for many-to-many relations (Thejaswi Puthraya, Matt Westcott)
Expand Down
12 changes: 12 additions & 0 deletions modelcluster/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ def order_by(self, *fields):
sort_by_fields(results, fields)
return FakeQuerySet(self.model, results)

# a standard QuerySet will store the results in _result_cache on running the query;
# this is effectively the same as self.results on a FakeQuerySet, and so we'll make
# _result_cache an alias of self.results for the benefit of Django internals that
# exploit it
def _get_result_cache(self):
return self.results

def _set_result_cache(self, val):
self.results = list(val)

_result_cache = property(_get_result_cache, _set_result_cache)

def __getitem__(self, k):
return self.results[k]

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='django-modelcluster',
version='3.0',
version='3.0.1a1',
description="Django extension to allow working with 'clusters' of models as a single unit, independently of the database",
author='Matthew Westcott',
author_email='matthew.westcott@torchbox.com',
Expand Down
29 changes: 27 additions & 2 deletions tests/tests/test_cluster_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from django.utils.six import text_type

from django.test import TestCase
from tests.models import Band, BandMember, Album, Restaurant
from tests.models import Band, BandMember, Album, Restaurant, Article, Author, Category
from modelcluster.forms import ClusterForm
from django.forms import Textarea, CharField
from django.forms import Textarea, CharField, ModelForm
from django.forms.widgets import TextInput

import datetime
Expand Down Expand Up @@ -497,3 +497,28 @@ class Meta:

self.assertIn(text_type(form.media['js']), 'test.js')
self.assertIn(text_type(form.media['css']), 'test.css')


class FormWithM2MTest(TestCase):
def test_render_form_with_m2m(self):
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['title', 'authors', 'categories']

author1 = Author.objects.create(name='James Joyce')
author2 = Author.objects.create(name='Charles Dickens')
article = Article.objects.create(
title='Test article',
authors=[author1]
)

form = ArticleForm(instance=article)
html = form.as_p()
self.assertIn('Test article', html)

article.authors.add(author2)

form = ArticleForm(instance=article)
html = form.as_p()
self.assertIn('Test article', html)

0 comments on commit 5c5ebdc

Please sign in to comment.