Skip to content

Commit

Permalink
Merge pull request #135 from karolyi/master
Browse files Browse the repository at this point in the history
Fix when selecting with an MSFList
  • Loading branch information
blag committed May 24, 2024
2 parents a571073 + dee5c6f commit 2ae2103
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
# and https://docs.djangoproject.com/en/4.2/faq/install/#what-python-version-can-i-use-with-django
matrix:
include:
- { dj: "4.1.*", py: "3.8" }
- { dj: "4.1.*", py: "3.11" }
- { dj: "4.2.*", py: "3.8" }
- { dj: "4.2.*", py: "3.12" }
- { dj: "5.0.*", py: "3.10" }
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
development (unreleased)
-------------------
* Fix error with Django>=4.1.3: <https://github.com/goinnn/django-multiselectfield/pull/135>

0.1.12 (2020-02-20)
-------------------

Expand Down
19 changes: 19 additions & 0 deletions example/app/test_msf_select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test.testcases import TestCase

from .models import Book


class MsfSelectTestCase(TestCase):
fixtures = ['app_data.json']

def test_valid_select(self):
"""
Should be able to use a multiselectfield result to select
See
https://github.com/goinnn/django-multiselectfield/pull/135
"""
book = Book.objects.first()
result = Book.objects.filter(categories=book.categories).only('pk')
self.assertEqual(len(result), 1)
self.assertEqual(result[0].pk, book.pk)
self.assertIn(member='1,3,5', container=str(result.query))
11 changes: 0 additions & 11 deletions multiselectfield/db/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ def wrapper(cls):
return wrapper


class MSFList(list):

def __init__(self, choices, *args, **kwargs):
self.choices = choices
super(MSFList, self).__init__(*args, **kwargs)

def __str__(msgl):
l = [msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i) for i in msgl]
return ', '.join([str(s) for s in l])


class MultiSelectField(models.CharField):
""" Choice values can not contain commas. """

Expand Down
33 changes: 31 additions & 2 deletions multiselectfield/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this programe. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

from collections import UserList
from typing import Optional, Union

from django.db.models.sql.query import Query


class _FakeSqlVal(UserList):

contains_aggregate = False
contains_column_references = False
contains_over_clause = False

def __str__(self):
return ','.join(map(str, self))


class MSFList(list):

Expand All @@ -22,8 +39,20 @@ def __init__(self, choices, *args, **kwargs):
super(MSFList, self).__init__(*args, **kwargs)

def __str__(msgl):
msg_list = [msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i) for i in msgl]
return ', '.join([str(s) for s in msg_list])
msg_list = [
msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i)
for i in msgl]
return ', '.join(str(s) for s in msg_list)

def resolve_expression(
self, query: Query = None, allow_joins: bool = True,
reuse: Optional[bool] = None, summarize: bool = False,
for_save: bool = False) -> Union[list, _FakeSqlVal]:
if for_save:
result = _FakeSqlVal(self)
else:
result = list(self)
return result


def get_max_length(choices, max_length, default=200):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import codecs
import os
from setuptools import setup, find_packages

from setuptools import find_packages, setup


def read(*rnames):
Expand Down

0 comments on commit 2ae2103

Please sign in to comment.