Exception raised during haystack unit test - python

I'm using python 3.5.2, Django 1.9 and django-haystack 2.5.1.
I have some models:
class Annotation(TimeStampedModel):
... other attributes ...
entity = models.ForeignKey('another_app.Entity', blank=True, null=True)
class Entity(models.Model):
... other attributes ...
I'm using this configuration in order to override the HAYSTACK_CONNECTIONS settings variable with another one (TEST_INDEX) which redirects all tests to a different search index.
#override_settings(HAYSTACK_CONNECTIONS=TEST_INDEX, HAYSTACK_SIGNAL_PROCESSOR='haystack.signals.BaseSignalProcessor')
class SearchEnpointsTests(OurAPITestCase):
fixtures = ['test_data.json']
#classmethod
def setUpClass(cls):
super(SearchEnpointsTests, cls).setUpClass()
haystack.connections.reload('default')
call_command('update_index', interactive=False, verbosity=0)
#classmethod
def tearDownClass(cls):
return super(SearchEnpointsTests, cls).tearDownClass()
# call_command('clear_index', interactive=False, verbosity=0)
test_search(self):
...
And, as settings variables:
# haystack:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': ELASTICSEARCH_HOST,
'INDEX_NAME': 'haystack'
}
}
TEST_INDEX = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': ELASTICSEARCH_HOST,
'INDEX_NAME': 'test_index',
},
}
Before configuring haystack real-time, it worked, now that I changed the configuration with this one:
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
It raises an exception during testing:
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 160, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Annotation' object has no attribute '_entity_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/project_folder/.venv/lib/python3.5/site-packages/django/test/testcases.py", line 1036, in setUpClass
'database': db_name,
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 119, in call_command
return command.execute(*args, **defaults)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 100, in loaddata
self.load_label(fixture_label)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 158, in load_label
obj.save(using=self.using)
File "/project_folder/.venv/lib/python3.5/site-packages/django/core/serializers/base.py", line 201, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/base.py", line 745, in save_base
update_fields=update_fields, raw=raw, using=using)
File "/project_folder/.venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/signals.py", line 52, in handle_save
index.update_object(instance, using=using)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 278, in update_object
backend.update(self, [instance])
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/backends/elasticsearch_backend.py", line 168, in update
prepped_data = index.full_prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 208, in full_prepare
self.prepared_data = self.prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/indexes.py", line 199, in prepare
self.prepared_data[field.index_fieldname] = field.prepare(obj)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 205, in prepare
return self.convert(super(CharField, self).prepare(obj))
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 88, in prepare
values = self.resolve_attributes_lookup(current_objects, attrs)
File "/project_folder/.venv/lib/python3.5/site-packages/haystack/fields.py", line 108, in resolve_attributes_lookup
if not hasattr(current_object, attributes[0]):
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 169, in __get__
rel_obj = qs.get()
File "/project_folder/.venv/lib/python3.5/site-packages/django/db/models/query.py", line 387, in get
self.model._meta.object_name
apps.another_app.models.DoesNotExist: Problem installing fixture '/project_folder/fixtures/test_data.json': Entity matching query does not exist.

Related

Convert MagicMock object to something I can use in my test

I'm not familiar with Python testing, particularly with using mocks so this may be completely wrong.
I have this code in my test:
import os
import mock
from mock import Mock
from tests import unittest
from datetime import datetime
from ibm_botocore.session import Session
import logging
class TestS3Parameters(unittest.TestCase):
def setUp(self):
super(TestS3Parameters, self).setUp()
# Set the config file to something that doesn't exist so
# that we don't accidentally load a config.
os.environ['AWS_CONFIG_FILE'] = '~/.aws/config-missing'
def create_session(self, *args, **kwargs):
"""
Create a new session with the given arguments. Additionally,
this method will set the credentials file to the test credentials
used by the following test cases.
"""
kwargs['session_vars'] = {
'credentials_file': (
None, None,
os.path.join(os.path.dirname(__file__), 'test-credentials'),
None)
}
return Session(*args, **kwargs)
#mock.patch('ibm_botocore.credentials.Credentials')
def test_extended_endpoint(self, my_mock):
now = datetime.utcnow().strftime("%Y%m%d")
extended_listing_mock_response_basic = {
"IsTruncated": False,
"Buckets": [
{
"Name": "bucketnamefoo",
"CreationDate": str(now),
"LocationConstraint": "bar-standard"
}
],
"Owner": {
"DisplayName": "ownerfoo",
"ID": "idfoo"
}
}
s = self.create_session()
client = s.create_client('s3', region_name='us-east-1', aws_access_key_id='foo',
aws_secret_access_key='bar')
# Create a mock response.
self.mock_response = Mock()
self.mock_response = extended_listing_mock_response_basic
request = my_mock.list_bucket()
self.assertEquals(request['httpRequest']['method'], 'GET')
self.assertEquals(request['httpRequest']['path'], '/?extended')
if __name__ == "__main__":
unittest.main()
When I run it I get this error:
Traceback (most recent call last):
File "/core/.tox/py33/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
File "/core/tests/unit/test_s3_param.py", line 56, in test_extended_endpoint
request = client.list_bucket()
File "/core/ibm_botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/core/ibm_botocore/client.py", line 648, in _make_api_call
operation_model, request_dict, request_context)
File "/core/ibm_botocore/client.py", line 667, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "/core/ibm_botocore/endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "/core/ibm_botocore/endpoint.py", line 132, in _send_request
request = self.create_request(request_dict, operation_model)
File "/core/ibm_botocore/endpoint.py", line 116, in create_request
operation_name=operation_model.name)
File "/core/ibm_botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/core/ibm_botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/core/ibm_botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/core/ibm_botocore/signers.py", line 90, in handler
return self.sign(operation_name, request)
File "/core/ibm_botocore/signers.py", line 157, in sign
auth.add_auth(request)
File "/core/ibm_botocore/auth.py", line 425, in add_auth
super(S3SigV4Auth, self).add_auth(request)
File "/core/ibm_botocore/auth.py", line 368, in add_auth
signature = self.signature(string_to_sign, request)
File "/core/ibm_botocore/auth.py", line 349, in signature
request.context['timestamp'][0:8])
File "/core/ibm_botocore/auth.py", line 169, in _sign
sig = hmac.new(key, msg.encode('utf-8'), sha256).digest()
File "/Users/me/.pyenv/versions/2.7.8/lib/python2.7/hmac.py", line 136, in new
return HMAC(key, msg, digestmod)
File "/Users/me/.pyenv/versions/2.7.8/lib/python2.7/hmac.py", line 75, in __init__
self.outer.update(key.translate(trans_5C))
TypeError: must be convertible to a buffer, not MagicMock
How do I convert this to a type I can use?
I tried using request = my_mock.list_bucket() but it give me this error:
AssertionError: <MagicMock name='Credentials.list_buckets_extended().__getitem__().__getitem__()' id='4459954832'> != 'GET'
What I'm trying to do is create a mock client which I can use for my list_bucket call. I then hope to check it to ensure the request is a GET and the /?extended endpoint is used.

Django - Celery ValueError: Related model u'user.User' cannot be resolved

When I am executing celery task it is giving me:
ValueError: Related model u'user.User' cannot be resolved
The stacktrace is
Traceback (most recent call last):
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 375, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/celery/app/trace.py", line 632, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/prince/work/magneto/set/facebook_pages/tasks.py", line 23, in analyze_page
connected_facebook_page = get_connected_facebook_page(connected_facebook_page_id)
File "/Users/prince/work/magneto/set/facebook_pages/utils.py", line 49, in get_connected_facebook_page
return ConnectedUserPage.objects.get(id=connected_facebook_page_id)
File "/Users/prince/virtualenvs/set/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/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
num = len(clone)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
self._fetch_all()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
sql, params = self.as_sql()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 428, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 46, in pre_sql_setup
self.setup_query()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 37, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 194, in get_select
for c in self.get_default_columns():
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 569, in get_default_columns
column = field.get_col(alias)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1008, in get_col
return super(ForeignKey, self).get_col(alias, output_field or self.target_field)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 909, in target_field
return self.foreign_related_fields[0]
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 640, in related_fields
self._related_fields = self.resolve_related_fields()
File "/Users/prince/virtualenvs/set/lib/python2.7/site-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model u'user.User' cannot be resolved
Here ConnectedUserPage is a model with schema:
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey('user.User', on_delete=models.PROTECT)
page = models.ForeignKey(FacebookPage, on_delete=models.PROTECT)
page_details = jsonb.JSONField()
My versions are :
celery==4.1.1
django-celery-beat==1.1.1
django-celery-results==1.0.1
Django==1.11.13
In case I directly import User model from user app I am getting stuck in circular imports.
Any help would be appreciated, stuck in this loop for a quite while now.
Where's User defined? Is this Django's own user model? If that's the case you can use User model directly:
from django.contrib.auth import get_user_model
User = get_user_model()
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey(User, unique=True)
or you can also do
from django.conf import settings
class ConnectedUserPage(TimeStampedModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
If it is your own supplied User model, are you sure user is in INSTALLED_APPS?

Transaction fails when calling contract function with return value

I'm hitting some unintuitive behavior in solidity when one of my contracts calls another. I'm testing the contracts with web3py. Here's is the most minimal example I could come up with. Note that it may not actually be a minimal example because failures reasons aren't propagated from ethereum to web3py.
Foo.sol:
pragma solidity ^0.4.0;
import "./Bar.sol";
contract Foo {
Bar public bar;
function Foo(){
bar = new Bar();
}
function test() returns (uint) { return 1; }
function test2() {}
function execute() {
bar.run();
}
}
Bar.sol
pragma solidity ^0.4.0;
import "./Foo.sol";
contract Bar {
address bar_address;
function Bar(){
bar_address = msg.sender;
}
function run() {
Foo foo = Foo(bar_address);
// foo.test(); # fails
foo.test2(); # succeeds
}
}
test.py
from web3 import Web3, EthereumTesterProvider
import unittest
import os
from eth_tester.exceptions import TransactionFailed
import tests.utils.utils as utils
from web3.utils.filters import Filter
class TestMycroToken(unittest.TestCase):
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
CONTRACT_ROOT = os.path.join(PROJECT_ROOT, "contracts")
TEST_CONTRACT_ROOT = os.path.join(CONTRACT_ROOT, "test_contracts")
def test_foo(self):
w3 = Web3(EthereumTesterProvider())
proposal_contract, proposal_contract_address, proposal_contract_instance = utils.create_contract(
self.CONTRACT_ROOT, os.path.join(self.TEST_CONTRACT_ROOT, "Foo.sol"), "Foo", w3 )
proposal_contract_instance.execute(transact={'from': self.w3.eth.accounts[1]})
When Bar.run calls Foo.test2() the test passes, but when Foo.test() is called, the test fails.
utils.create_contract more or less does what's shown in the quickstart for web3py with some modifications to handle compiling multiple files.
I'm getting the following stack trace for the error:
/Users/paymahn/mycro/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_unittest_runner.py --target test_mycro_token.TestMycroToken.test_foo
Launching unittests with arguments python -m unittest test_mycro_token.TestMycroToken.test_foo in /Users/paymahn/mycro/tests
Ran 1 test in 0.692s
FAILED (errors=1)
Error
Traceback (most recent call last):
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/utils/formatting.py", line 85, in wrapper
return to_wrap(*args, **kwargs)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/backends/pyethereum/v16/main.py", line 439, in estimate_gas
transaction=transaction,
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/backends/pyethereum/v16/main.py", line 157, in _estimate_evm_transaction
return _send_evm_transaction(tester_module, evm, transaction_for_estimate)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/backends/pyethereum/v16/main.py", line 145, in _send_evm_transaction
evmdata=transaction.get('data', b''),
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/ethereum/tester.py", line 338, in send
return self._send(*args, **kwargs)["output"]
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/ethereum/tester.py", line 296, in _send
raise TransactionFailed()
ethereum.tester.TransactionFailed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/Users/paymahn/mycro/tests/test_mycro_token.py", line 125, in test_foo
proposal_contract_instance.execute(transact={'from': self.w3.eth.accounts[1]})
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/contract.py", line 777, in __call__
return self.__prepared_function(*args, **kwargs)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/contract.py", line 790, in __prepared_function
return getattr(self._function(*args), modifier)(modifier_dict)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/contract.py", line 1028, in transact
**self.kwargs)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/contract.py", line 1305, in transact_with_contract_function
txn_hash = web3.eth.sendTransaction(transact_transaction)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/eth.py", line 247, in sendTransaction
get_buffered_gas_estimate(self.web3, transaction),
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/utils/transactions.py", line 72, in get_buffered_gas_estimate
gas_estimate = web3.eth.estimateGas(gas_estimate_transaction)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/eth.py", line 288, in estimateGas
[transaction],
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/manager.py", line 103, in request_blocking
response = self._make_request(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/manager.py", line 86, in _make_request
return request_func(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/gas_price_strategy.py", line 18, in middleware
return make_request(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/formatting.py", line 21, in middleware
response = make_request(method, formatted_params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/attrdict.py", line 18, in middleware
response = make_request(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/formatting.py", line 21, in middleware
response = make_request(method, formatted_params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/normalize_errors.py", line 9, in middleware
result = make_request(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/validation.py", line 44, in middleware
return make_request(method, post_validated_params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/formatting.py", line 21, in middleware
response = make_request(method, formatted_params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/providers/eth_tester/middleware.py", line 315, in middleware
return make_request(method, [filled_transaction] + params[1:])
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/fixture.py", line 12, in middleware
return make_request(method, params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/middleware/formatting.py", line 21, in middleware
response = make_request(method, formatted_params)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/providers/eth_tester/main.py", line 46, in make_request
response = delegator(self.ethereum_tester, params)
File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/web3/providers/eth_tester/defaults.py", line 36, in call_eth_tester
return getattr(eth_tester, fn_name)(*fn_args, **fn_kwargs)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/main.py", line 466, in estimate_gas
raw_gas_estimate = self.backend.estimate_gas(raw_transaction)
File "/Users/paymahn/mycro/venv/lib/python3.6/site-packages/eth_tester/utils/formatting.py", line 88, in wrapper
raise old_to_new_exceptions[type(e)] from e
eth_tester.exceptions.TransactionFailed

django : loading fixtures with natural foreignkey fails with 'ValueError: invalid literal for int() with base 10'

My models are ...
class StateManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class DistrictManager(models.Manager):
def get_by_natural_key(self, name, state):
return self.get(name=name, state=state)
class State(models.Model):
class Meta:
verbose_name = "State"
verbose_name_plural = "States"
permissions = (
('access_state', 'Can access States'),
)
COUNTRIES = (
('India', 'India'),
('USA', 'USA'),
('Thailand', 'Thailand'),
)
# Managers
objects = StateManager()
# Database fields
name = models.CharField(
'Name',
max_length=100,
unique=True,
help_text='''
100 chars max
'''
)
code = models.CharField(
'Code',
max_length=10,
unique=True,
help_text='''
10 chars max
''',
null=True, blank=True
)
country = models.CharField(
max_length=50,
default="India",
choices=COUNTRIES,
blank=False,
null=False
)
def __str__(self):
return self.name
def natural_key(self):
return (self.name,)
class District(models.Model):
class Meta:
verbose_name = "District"
verbose_name_plural = "Districts"
unique_together = (
(
"state",
"name"
),
)
# Managers
objects = DistrictManager()
# Database fields
name = models.CharField(
'Name',
max_length=100,
help_text='''
100 chars max
'''
)
code = models.CharField(
'Code',
max_length=10,
help_text='''
10 chars max
''',
null=True, blank=True
)
state = models.ForeignKey(State)
def __str__(self):
return self.name
def natural_key(self):
return (self.name,) + self.state.natural_key()
natural_key.dependencies = ['parties.state']
My fixture for District model is ....
[
{
"model": "parties.district",
"fields": {
"name": "North Andaman",
"state": [
"Andaman and Nicobar"
],
"code": "NA"
}
},
{
"model": "parties.district",
"fields": {
"name": "South Andaman",
"state": [
"Andaman and Nicobar"
],
"code": "SA"
}
}
]
In fact, the fixture is generated by django's 'dumpdata' itself.
But, while trying to load the fixture, I am getting the following error ...
ValueError: invalid literal for int() with base 10: 'Andaman and Nicobar'
The full trace is given below ...
Traceback (most recent call last):
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/json.py", line 79, in Deserializer
for obj in PythonDeserializer(objects, **options):
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/python.py", line 157, in Deserializer
obj = base.build_instance(Model, data, db)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/base.py", line 195, in build_instance
obj.pk = Model._default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
File "/home/parijath/Projects/django_projects/webportal18_multipleapps/parties/models.py", line 17, in get_by_natural_key
return self.get(name=name, state=state)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 325, in get
clone = self.filter(*args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1310, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1338, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1200, in build_filter
lookups, value)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/related.py", line 1761, in get_lookup_constraint
lookup_class(target.get_col(alias, source), val), AND)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py", line 101, in __init__
self.rhs = self.get_prep_lookup()
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Andaman and Nicobar'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 100, in loaddata
self.load_label(fixture_label)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py", line 151, in load_label
for obj in objects:
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/json.py", line 85, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/json.py", line 79, in Deserializer
for obj in PythonDeserializer(objects, **options):
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/python.py", line 157, in Deserializer
obj = base.build_instance(Model, data, db)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/base.py", line 195, in build_instance
obj.pk = Model._default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
File "/home/parijath/Projects/django_projects/webportal18_multipleapps/parties/models.py", line 17, in get_by_natural_key
return self.get(name=name, state=state)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 325, in get
clone = self.filter(*args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1310, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1338, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1200, in build_filter
lookups, value)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/related.py", line 1761, in get_lookup_constraint
lookup_class(target.get_col(alias, source), val), AND)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py", line 101, in __init__
self.rhs = self.get_prep_lookup()
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/parijath/Projects/django_projects/webportal18_multipleapps/parties/fixtures/districts-2.json': invalid literal for int() with base 10: 'Andaman and Nicobar'
Where am I doing wrong ?
Finally, I found the mistake I am doing.
Instead of ....
class DistrictManager(models.Manager):
def get_by_natural_key(self, name, state):
return self.get(name=name, state=state)
I've modified the code to ...
class DistrictManager(models.Manager):
def get_by_natural_key(self, name, state):
return self.get(name=name, state__name=state)
The main point here is state__name=state (not state=state) which I missed earlier.

How to create self-referencing EndpointsModel

I'm trying to create following self-referencing EndpointsModel (the trick with _fix_up_properties() is taken from here: https://groups.google.com/forum/#!topic/appengine-ndb-discuss/1FmgEVK7JNM):
class EventFieldSchema(EndpointsModel):
name = ndb.StringProperty(required=True)
type = msgprop.EnumProperty(EventType, required=True)
EventFieldSchema.nested_fields = ndb.LocalStructuredProperty(EventFieldSchema,repeated=True)
EventFieldSchema._fix_up_properties()
This works for datastore model, but unfortunately, the nested_fields field won't be included into ProtoRPC message.
I've tried to manually specify message fields schema, by adding at the end following line:
EventFieldSchema._message_fields_schema = ('name', 'type', 'nested_fields')
but then app-engine fails, going into a loop, trying turn EventFieldSchema into ProtoRPC field:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/main.py", line 3, in <module>
from er.api.event import EventRegistryApi
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/er/api/event.py", line 17, in <module>
class EventRegistryApi(remote.Service):
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/er/api/event.py", line 23, in EventRegistryApi
name='%s.insert' % RESOURCE_NAME)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/util.py", line 170, in positional_wrapper
return wrapped(*args, **kwargs)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 1359, in method
kwargs[REQUEST_MESSAGE] = cls.ProtoModel(fields=request_fields)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 1031, in ProtoModel
allow_message_fields=allow_message_fields)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 969, in _MessageFields
proto_attr = to_proto(prop, field_index)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/utils.py", line 137, in StructuredPropertyToProto
property_proto = property_proto_method()
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 1031, in ProtoModel
allow_message_fields=allow_message_fields)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 969, in _MessageFields
proto_attr = to_proto(prop, field_index)
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/utils.py", line 137, in StructuredPropertyToProto
property_proto = property_proto_method()
File "/base/data/home/apps/s~project/eventregistry:dev.380885914276541023/endpoints_proto_datastore/ndb/model.py", line 1031, in ProtoModel
Is this a bug in EndpointsModel? What is the "proper" way of defining self-referencing EndpointsModels?
Having the same issue with self-referencing an EndpointsModel:
class UnitsProtoModel(EndpointsModel):
""" ProtoRPC Model for storing/retrieving a unit. """
_message_fields_schema = ('id', 'uic', 'parent_id', 'branch_id', 'name')
uic = ndb.StringProperty(required=True)
parent_id = ndb.StringProperty(required=True, default=None)
branch_id = ndb.StringProperty(required=True)
name = ndb.StringProperty(required=True)
abbreviated_name = ndb.StringProperty(default="")
flagged = ndb.BooleanProperty(default=False)
message = ndb.StringProperty(default="")
unit_created_at = ndb.DateTimeProperty(auto_now_add=True)
class UnitsCollection(EndpointsModel):
items = messages.MessageField(UnitsProtoModel, 1, repeated=True)
Error msg:
`File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine- default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/util.py", line 170, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1531, in init
raise FieldDefinitionError('Invalid message class: %s' % message_type)
FieldDefinitionError: Invalid message class:
UnitsProtoModel<abbreviated_name=StringProperty('abbreviated_name', default=''), branch_id=StringProperty('branch_id', required=True), flagged=BooleanProperty('flagged', default=False), message=StringProperty('message', default=''), name=StringProperty('name', required=True), owner=UserProperty('owner'), parent_id=StringProperty('parent_id', required=True), uic=StringProperty('uic', required=True), unit_created_at=DateTimeProperty('unit_created_at', auto_now_add=True)>`

Categories