Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when django-admin-caching is enabled, update_or_create() function always return None #10

Closed
forance opened this issue Jul 10, 2017 · 9 comments

Comments

@forance
Copy link

forance commented Jul 10, 2017

when I include this package in the INSTALLED_APPS in the setting.py, running update_or_create() method from object queryset API always return "'NoneType' object has no attribute 'find'". i pretty sure the object is exist because if i use get_or_create() then it works.

Not sure what happen but i really like this package, it really make admin running faster.

Thanks

Nick

@kairichard
Copy link
Contributor

@forance thanks for your report. We will look into the issue. //cc @zsoldosp

@kairichard
Copy link
Contributor

@forance Can you paste the entire stacktrace, that would help tremendously. Thanks in advance.

@forance
Copy link
Author

forance commented Jul 26, 2017

okay i will try my best to describe the issue.
I have two database

Database A: MS-SQL
Database B: Postgres
Django will regularly read data from database A and write to database B using Django-Q(task manager, similar to Celery)

Below is the code, I deleted some unnecessary line.

`

import pymssql

try:
    conn = pymssql.connect(connect_db.host, connect_db.user, connect_db.password, "winQHs", timeout)
    cursor = conn.cursor()

except Exception as e:
        return (
                 'Connection failed on {now} with Error {error}'
                 .format(now=datetime.datetime.today(), error=e.message)
        )

cursor.execute("""
select sc.DocNo, sc.CustID, sc.DDate, sc.Curncy, sc.OrdAmt, sc.RvdAmt, sc.Creater,sc.CDate,sc.Payment, sc.Updater, sc.UDate
from dbo.SC AS sc
where year(sc.CDate)= {0} or year(sc.UDate) = {0} 
ORDER BY DDate DESC;
""".format(year))

for index, item in enumerate(cursor, start=1):
    try:
          customer, created = Customer.objects.get_or_create(company_name=item[1])
          updated_values = {'customer':item[1], 'customer_obj':customer,'ship_date':item[2].......}
          obj, created = Orders.objects.update_or_create(order_id=item[0],defaults=updated_values )
          if created:
            create += 1
          else:
            duplicate += 1
            pass
          
    except Exception as e:
        logger.info("pi error{0}, {1} {2}".format(e.message, item[0], item[1],))
        continue            
conn.close()

`

The problem happens with line
obj, created = Orders.objects.update_or_create(order_id=item[0],defaults=updated_values )

Here is the error message..

15:48:00 [Q] INFO Process-1:1 processing [florida-tennessee-juliet-happy]
pi error'NoneType' object has no attribute 'find', 60873 HERR
pi error'NoneType' object has no attribute 'find', 71615 MAPLES
pi error'NoneType' object has no attribute 'find', 88741 KONG
pi error'NoneType' object has no attribute 'find', 88740 VAC
pi error'NoneType' object has no attribute 'find', 60872 weston
pi error'NoneType' object has no attribute 'find', 06095 DEGI
pi error'NoneType' object has no attribute 'find', 50339 JI
pi error'NoneType' object has no attribute 'find', 06094 PRO
15:48:00 [Q] ERROR Failed [florida-tennessee-juliet-happy] - 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

I do not know how this issue related to admin-caching, but the problem occurs when admin-caching is enabled.

Let me know if you need more information, I am willing to help.

Thanks

@kairichard
Copy link
Contributor

@forance thank you very much for the detailed report. This looks like a tricky problem. We will keep you posted.

@kairichard
Copy link
Contributor

kairichard commented Jul 27, 2017

@forance In order to fully understand where the problem is coming from I would need a sample stacktrace like this where 'NoneType' object has no attribute 'find' is the last line.
Similar to this:

Traceback (most recent call last):
  File "/var/www/xxx/releases/1/app/bin/../django-sites/xxx/manage.py", line 50, in <module>
    execute()
  File "/var/www/xxx/releases/1/app/bin/../django-sites/xxx/manage.py", line 23, in execute
    execute_from_command_line()
  File "/var/www/xxx/current/venv-latest/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/var/www/xxx/current/venv-latest/lib/python2.7/site-packages/django/core/management/__init__.py", line 316, in execute
    settings.INSTALLED_APPS
  File "/var/www/xxx/current/venv-latest/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/www/xxx/current/venv-latest/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/www/xxx/current/venv-latest/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/var/www/xxx/releases/1/app/django-sites/xxx/settings/base.py", line 515, in <module>
    composer.load_and_set_from('aspects.testing')
  File "/var/www/xxx/releases/1/app/django-sites/xxx/settings/base.py", line 515, in <module>
    composer.load_and_set_from('aspects.testing')

Thanks in advance.

@forance
Copy link
Author

forance commented Aug 2, 2017

hello,
Sorry for the late reply, was caught up in something

Here is the stacktrace. Let me know if you need more info. Thanks

Traceback (most recent call last):
File "/Users/nicktang/Documents/django_web_server/djangoq_demo/order_reminder/tasks.py", line 134, in updb
obj, created = Orders.objects.update_or_create(order_id=item[0],creater=item[6],defaults=updated_values )
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/db/models/query.py", line 493, in update_or_create
obj.save(using=self.db)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/field_history/tracker.py", line 93, in save
ret = original_save(**kwargs)
File "/Users/nicktang/Documents/django_web_server/djangoq_demo/order_reminder/models.py", line 65, in save
super(Orders, self).save(*args, **kwargs)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/db/models/base.py", line 833, in save_base
update_fields=update_fields, raw=raw, using=using)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 191, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django_admin_caching/signals.py", line 6, in auto_delete_from_cache_on_model_post_save
AutoKeyedCache(result=instance).delete()
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django_admin_caching/caching.py", line 94, in delete
self.cfg.cache.delete(key=self.ck.key)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django_admin_caching/caching.py", line 42, in key
self.i18n_l10n_prefix,
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django_admin_caching/caching.py", line 57, in i18n_l10n_prefix
locale = translation.to_locale(lang)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/utils/translation/init.py", line 202, in to_locale
return _trans.to_locale(language)
File "/Users/nicktang/.virtualenvs/proj_q/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 71, in to_locale
p = language.find('-')
AttributeError: 'NoneType' object has no attribute 'find'

@kairichard
Copy link
Contributor

@forance Thanks a bunch, that explains a lot. Looks like a a deactivated translation causes the problem. We will write a fix for it.
//cc @zsoldosp

jm9e added a commit to Bitspark/django-admin-caching that referenced this issue Aug 8, 2017
kairichard added a commit that referenced this issue Aug 10, 2017
Fix for issue #10 (AttributeError: NoneType for lang)
@kairichard
Copy link
Contributor

@forance a new version has been released

@forance
Copy link
Author

forance commented Aug 13, 2017

@kairichard

Thanks for the help. It works!! great, I can enable caching again.

Big thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants