modlinltd/django-advanced-filters

ImportError: cannot import name 'six'

Closed this issue · 16 comments

When I ran "python manage.py runserver", I got this error.
I'm using django version: 3.0.6

(py36) Thanatchons-MacBook-Pro% python manage.py runserver
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/management/init.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/advanced_filters/models.py", line 6, in
from .q_serializer import QSerializer
File "/Users/thanatchonchongmankhong/anaconda3/envs/py36/lib/python3.6/site-packages/advanced_filters/q_serializer.py", line 6, in
from django.utils import six
ImportError: cannot import name 'six'

Hey, can you try installing the package from develop branch directly? I didn't get the chance to tag and release develop into pypi yet (was hoping to add more fixes) and I believe it may be fixing your issue.

If it fixes it, I'll release a small version right away.

Thank you. "develop" branch fixes this.

I also wonder if we can add calculated fields.

Where would the query for these calculated fields come from?

class Report(TimeStampMixin):
    id = models.CharField(max_length = 10,primary_key=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    start_date = models.DateField(verbose_name='date')

class ReportDetail(TimeStampMixin):
    report = models.ForeignKey(Report, on_delete=models.CASCADE)
    invoice_date = models.DateField(verbose_name='date')
    price = models.FloatField(default=0)

Supposed that I would like to create advance filter in Report page to filter only when summation of price in ReportDeail is greater than some certain amout, how can I achieve this?

To add calculated fields to the Report table, I would do this:

class ReportAdmin(AdminAdvancedFiltersMixin, admin.ModelAdmin):
    search_fields = ['id','company__name']
    list_display = ('id','company'','price_sum','start_date')

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.annotate(
            _price_sum = Sum("reportdetail__price"),
        )
        return queryset
    def price_sum(self, obj):
        return obj._price_sum
    price_sum.admin_order_field = '_price_sum'
    price_sum.short_description = 'Total Price'

While it is possible to filter on annotations, I'm not sure how we could pick up the field type and valid values for fields. We could of course make it an "wildcard" or "any query you like" type of field, but...

I was avoiding adding support for "custom query" field that will allow users to add any query, due to security implications of this. We could theoretically make it a "wildcard_query" permission that will explicitly allow a user to do this, but it's still risky of course.

@thanatchon36 would you like to take a stab at a PR to contribute this feature?

@asfaltboy Yes, sure.

same issue, is there a fix yet? thanks! pip install git+https://github.com/modlinltd/django-advanced-filters.git@develop didn't work for me. django 3.0 - six, django.utils.functional, and other packages are broken from import. @asfaltboy

Version 1.2.0 is now live on pypi, please install it and let us know if there's any issues

with 1.2.0

I"m still getting...

.......
  File "C:\Users\davida\.virtualenvs\photovote-MOt0CBHJ\lib\site-packages\advanced_filters\urls.py", line 3, in <module>
    from advanced_filters.views import GetFieldChoices
  File "C:\Users\davida\.virtualenvs\photovote-MOt0CBHJ\lib\site-packages\advanced_filters\views.py", line 11, in <module>
    from braces.views import (CsrfExemptMixin, StaffuserRequiredMixin,
  File "C:\Users\davida\.virtualenvs\photovote-MOt0CBHJ\lib\site-packages\braces\views\__init__.py", line 3, in <module>
    from ._access import (
  File "C:\Users\davida\.virtualenvs\photovote-MOt0CBHJ\lib\site-packages\braces\views\_access.py", line 13, in <module>
    from django.utils import six
ImportError: cannot import name 'six' from 'django.utils' (C:\Users\davida\.virtualenvs\photovote-MOt0CBHJ\lib\site-packages\django\utils\__init__.py)

with python manage.py runserver

Hey @dangelsaurus , thanks for reporting. I just tried downloading the tar.gz from pypi, extracted and my q_serializer.py looks different from what you report above ^ (my imports end at line 10):

"""This is a module to serializers/deserializes Django Q (query) object."""
from datetime import datetime, date
import base64
import time

import six
from django.db.models import Q
from django.core.serializers.base import SerializationError

import simplejson as json

Would you mind confirming that after installing 1.2.0 from pypi your app in site-packages is unaltered? Can you try pip uninstall -y django-admin-filters first, and confirm there isn't any py/pyc files left?

Ok, so it looks like I might be facing an issue with another package not being happy with Python 3.8.... so 1.2.0 actually might not be getting installed correctly by Pipenv. Let me play around and see what I can do to resolve that first and I'll pop back in here if I still have issues with this package. Appreciate the quick reply!

Exception in thread django-main-thread: Traceback (most recent call last): File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner self.run() File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run self.check(display_num_errors=True) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check include_deployment_checks=include_deployment_checks, File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/management/base.py", line 382, in _run_checks return checks.run_checks(**kwargs) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks new_errors = check(app_configs=app_configs) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique all_namespaces = _load_all_namespaces(resolver) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces url_patterns = getattr(resolver, 'url_patterns', []) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 588, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 581, in urlconf_module return import_module(self.urlconf_name) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/valeriozhang/trubillapi/MySite/urls.py", line 50, in <module> url(r'^advanced_filters/', include('advanced_filters.urls')) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include urlconf_module = import_module(urlconf_module) File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/advanced_filters/urls.py", line 3, in <module> from advanced_filters.views import GetFieldChoices File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/advanced_filters/views.py", line 11, in <module> from braces.views import (CsrfExemptMixin, StaffuserRequiredMixin, File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/braces/views/__init__.py", line 21, in <module> from ._forms import ( File "/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/braces/views/_forms.py", line 6, in <module> from django.utils.functional import curry, Promise ImportError: cannot import name 'curry' from 'django.utils.functional' (/Users/valeriozhang/trubillapi/venv/lib/python3.7/site-packages/django/utils/functional.py)

still having the above issue. any ideas? Using django 3, but looks like curry has been removed in this version.

Thanks @valeriozhang , which version of braces do you have installed?

For tests to pass, django-advanced-filters had to require a particular version of Django-braces, specifically <2.0 since the new version broke some stuff.

I could be that there's an untested import in advanced_filters/views.py so that our test suite does not catch it?

Ok yeah. looks liek its fixed, thank you very much. Just need to pip install --upgrade django-braces.

I'm still getting this error with 1.2.0 installed from Pip. It can be resolved by installing the latest Django-braces over-top of the version installed as a dependency of django-advanced-filters but that shouldn't be required. Is there someway we can change the dependencies of django-advanced-filters in PIP to avoid this? I'm happy to help if that's useful, but I haven't done much package development.

@mitchellpalmer what is the version of django-braces after installing django-advanced-filters (without installing latest django-braces)? What is your Django version? You can uninstall braces, reinstall advanced-filters and pip freeze to find out. Feel free to paste it here and I'll take a look