I have the following factory:
class ContactFactory(DjangoModelFactory):
name = Faker('company')
industry = Iterator(Industry.objects.all())
class Meta:
model = 'sales.contact'
#post_generation
def requested_devices(self, create, extracted, **kwargs):
if create:
self.requested_devices.add(MSize.objects.first())
And I'm trying to write a test such as:
class TestUserCanAskQuestion(TestCase):
#classmethod
def setUpTestData(cls):
call_command('insert_initial_data')
def setUp(self):
self.contact = ContactFactory()
But everytime I run the test, it results in "StopIteration" error.
Here is the full stack trace:
ERROR: test_dummy (comminquiry.tests.test_views.TestUserCanAskQuestion)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/c/Users/Alire/Projects/mm/mm-bpmui/src/comminquiry/tests/test_views.py", line 32, in test_dummy
a = ContactFactory.build()
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 546, in build
return cls._generate(enums.BUILD_STRATEGY, kwargs)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 500, in _generate
return step.build()
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 272, in build
step.resolve(pre)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 221, in resolve
self.attributes[field_name] = getattr(self.stub, field_name)
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 375, in __getattr__
extra=context,
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/declarations.py", line 196, in evaluate
value = next(iter(self.iterator))
File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/utils.py", line 136, in __iter__
value = next(self.iterator)
StopIteration
----------------------------------------------------------------------
Ran 1 test in 2.433s
If I move the ContactFactory() outside of class, the error disappears.
I'm I missing something? or it's a bug in factory boy or django?
(I'm using factory_boy==2.11.1 and django==2.1.2)
As #dirkgroten had suggested, one the fields was returning an empty queryset. This was the root cause of the error.
Related
I am trying to use a parent class as a blueprint for new classes.
E.g. the FileValidator contains all generic attributesand methods for a generic file. Then I want to create for example a ImageValidator inheriting everything from the FileValidator but with additional, more specific attribtues, methods. etc. In this example the child class is called: FileValidatorPlus
My understanding was, that if I inherit the parent class I can just plug-in more attributes/methods without repeating anything, like just adding min_size. But the following code gives: TypeError: FileValidatorPlus.__init__() got an unexpected keyword argument 'max_size'
class File:
def __init__(self, file_size=100):
self.file_size = file_size
class FileValidator(object):
error_messages = {'max_size': 'some error msg'}
def __init__(self, max_size=None):
self.max_size = max_size
def __call__(self, file):
print(self.max_size > file.file_size)
class FileValidatorPlus(FileValidator):
error_messages = {'min_size': 'some other error msg'}
def __init__(self, min_size=None):
super(FileValidatorPlus, self).__init__()
self.error_messages.update(super(FileValidatorPlus, self).error_messages)
self.min_size = min_size
validator = FileValidatorPlus(max_size=10, min_size=20)
print(validator.error_messages)
print(validator.max_size)
print(validator.min_size)
Full Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
TypeError: FileValidatorPlus.__init__() got an unexpected keyword argument 'max_size'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_code_executor.py", line 108, in add_exec
more, exception_occurred = self.do_add_exec(code_fragment)
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 90, in do_add_exec
command.run()
File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_console_types.py", line 35, in run
self.more = self.interpreter.runsource(text, '<input>', symbol)
File "/usr/local/lib/python3.10/code.py", line 74, in runsource
self.runcode(code)
File "/usr/local/lib/python3.10/code.py", line 94, in runcode
self.showtraceback()
File "/usr/local/lib/python3.10/code.py", line 148, in showtraceback
sys.excepthook(ei[0], ei[1], last_tb)
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 112, in info
traceback.print_exception(type, value, tb)
NameError: name 'traceback' is not defined
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 284, in process_exec_queue
interpreter.add_exec(code_fragment)
File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_code_executor.py", line 132, in add_exec
return more, exception_occurred
UnboundLocalError: local variable 'exception_occurred' referenced before assignment
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 511, in <module>
pydevconsole.start_server(port)
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 407, in start_server
process_exec_queue(interpreter)
File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 292, in process_exec_queue
traceback.print_exception(type, value, tb, file=sys.__stderr__)
UnboundLocalError: local variable 'traceback' referenced before assignment
Process finished with exit code 1
You need to add max_size as a parameter in your FileValidatorPlus constructor.
If you want FileValidatorPlus to inherit this field from the super's constructor, you must not define a __init__ method.
In other words, if you want FileValidatorPlus to inherit max_size parameter from FileValidator, you should delete __init__ because the subclass only inherits constructor properties when they don't have their own constructor.
But in your case I think that it would be a better solution just to add min_size=None to FileValidatorPlus.__init__:
def __init__(self, max_size=None, min_size=None):
super(FileValidatorPlus, self).__init__(max_size)
I got also different error :
TypeError: __init__() got an unexpected keyword argument 'max_size'
and the solution would be :
def __init__(self, max_size=None, min_size=None):
super(FileValidatorPlus, self).__init__(max_size)
Output:
{'min_size': 'some other error msg', 'max_size': 'some error msg'}
10
20
I'm using Django 2, Python 3.7 and the django-address module (https://pypi.org/project/django-address/). I'm trying to insert some seed data. I have this YAML ...
- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state:
name: IL
country:
- United States
When I run my seed command
python manage.py loaddata maps/fixtures/seed_data.yaml
I get this error ...
localhost:web davea$ python manage.py loaddata maps/fixtures/seed_data.yaml
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 923, in to_python
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 157, in Deserializer
data[field.attname] = model._meta.get_field(field_name).to_python(field_value)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 928, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'{'name': 'IL', 'country': ['United States']}' value must be an integer."]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
self.load_label(fixture_label)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
for obj in objects:
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 159, in Deserializer
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/davea/Documents/workspace/chicommons/maps/web/maps/fixtures/seed_data.yaml': ["'{'name': 'IL', 'country': ['United States']}' value must be an integer."]: (address.locality:pk=1) field_value was '{'name': 'IL', 'country': ['United States']}'
I have added this in the file maps/monkey_patching.py to help with auto-creating the entities based on unique identifiers ...
from address import State
from address import Country
def country_get_by_natural_key(self, name):
return self.get_or_create(name=name)[0]
def state_get_by_natural_key(self, name, country_id):
return self.get_or_create(name=name, country_id=country_id)[0]
Country.add_to_class("get_by_natural_key",country_get_by_natural_key)
State.add_to_class("get_by_natural_key",state_get_by_natural_key)
Edit: including stack trace per Shivam's request ...
(venv) localhost:maps davea$ python web/manage.py loaddata web/maps/fixtures/seed_data.yaml
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 923, in to_python
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 157, in Deserializer
data[field.attname] = model._meta.get_field(field_name).to_python(field_value)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 928, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'['IL', 'United States']' value must be an integer."]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "web/manage.py", line 21, in <module>
main()
File "web/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
self.load_label(fixture_label)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
for obj in objects:
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 159, in Deserializer
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/davea/Documents/workspace/chicommons/maps/web/maps/fixtures/seed_data.yaml': ["'['IL', 'United States']' value must be an integer."]: (address.locality:pk=1) field_value was '['IL', 'United States']'
Issue with natural key
get_by_natural_key method is available on the manager level but you are adding method on model level. So instead of above, you need to do like this:
from django.db import models
from address.models import State
class CustomManager(models.Manager):
def get_by_natural_key(self, name):
return self.get_or_create(name=name)[0]
State.add_to_class('objects', CustomManager())
Issue with fixtures
Nested data of fixtures should only contain args and not kwargs. Check Is there a way in a seed_data.yaml file to autogenerate models on which first model is dependent upon? for more detail. And after that, you need to design your get_by_natural_key function accordingly. So for locality model fixtures would look like
- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state: ['I', 'United States']
And overrided manager would be
class CustomManager(models.Manager):
def get_by_natural_key(self, state_name, country):
country = Country.objects.get_or_create(name=country)[0]
return State.objects.get_or_create(name=state_name, country=country)[0]
setattr(State._meta, 'default_manager', CustomManager())
I would offer an alternative approach, which is just to write your own management command to import this data, instead of trying to make loaddata work - I think this is potentially much less effort.
This command works for the sample data you've provided - it may need some minor adjustments if you have more than just address.locality objects in your YAML file:
import yaml
from django.core.management.base import BaseCommand
from address.models import Country, State, Locality
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('file', nargs=1, type=str)
def handle(self, *args, **options):
f = options['file'][0]
with open(f, 'r') as yamlfile:
data = yaml.load(yamlfile)
for row in data:
if row['model'] == 'address.locality':
state_name = row['fields']['state']['name']
country_name = row['fields']['state']['country'][0]
country_obj, _ = Country.objects.get_or_create(name=country_name)
state_obj, _ = State.objects.get_or_create(country=country_obj, name=state_name)
Locality.objects.get_or_create(
pk=row['pk'],
state=state_obj,
postal_code=row['fields']['postal_code'],
name=row['fields']['name']
)
(Side note - the format of your YAML is a little weird, as it provides a list for country. I would have thought a state can only belong to a single country?).
In the source code of this package, Locality.state field is related to State model by a foreignKey. So, your data fixture state field must be an integer of the related table name.
See django-address source code
PS: In your case you must build all the related models that Locality model is related to. If not you'll have a data integrity error ... And all bunch of other errors.
import pickle
class CustomEx(Exception):
def __init__(self, *args, **kwargs):
self.a = kwargs.get('a')
assert self.a, "a must present"
s = pickle.dumps(CustomEx(a='a'))
pickle.loads(str(s)) # this fails
I'm using python version 2.7.13. Please advise how can I make it work. It fails with following stack trace:
Traceback (most recent call last):
File "/Users/app/tmp/pickle_user_purchase_required.py", line 10, in <module>
pickle.loads(str(s))
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1388, in loads
return Unpickler(file).load()
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1139, in load_reduce
value = func(*args)
File "/Users/app/tmp/pickle_user_purchase_required.py", line 6, in __init__
assert self.a, "a must present"
AssertionError: a must present
I try to set some config attributes with an customm section. Then when I want to read the attributes I get an NoSectionError. The example below is absolute basic and may don't make sense in this way. But I need to read the config in the build method.
Versions used:
Python 2.7
kivy 1.9.1
Example
class MyTestApp(App):
def build(self):
Config.get('user_settings', 'strategy1')
return FloatLayout()
def build_config(self, config):
config.setdefaults('user_settings',
{
'strategy1': 'example',
'strategy2': 'example'
})
MyTestApp().run()
Result in the following exception:
Traceback (most recent call last):
File "test/main.py", line 59, in <module>
MyTestApp().run()
File "C:\Python27\lib\site-packages\kivy\app.py", line 802, in run
root = self.build()
File "test/main.py", line 17, in build
Config.get('user_settings', 'strategy1')
File "C:\Python27\lib\site-packages\kivy\config.py", line 433, in get
value = PythonConfigParser.get(self, section, option, **kwargs)
File "C:\Python27\lib\ConfigParser.py", line 607, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'user_settings'
Inside the app you'll have to use self.config instead of Config
I am following the code in the readthedocs (http://django-permission.readthedocs.org/en/latest/). Difficulty starts at Apply permission logic section of the docs. All works fine as I cut&paste
art1 = Article.objects.create(
title="Article 1",
body="foobar hogehoge",
author=user1
)
The following traceback is generated
Traceback (most recent call last):
File "<console>", line 4, in <module>
File "C:\Django\test_permissions\lib\site-packages\django\db\models\manager.py", line 157, in create
return self.get_queryset().create(**kwargs)
File "C:\Django\test_permissions\lib\site-packages\django\db\models\query.py", line 320, in create
obj = self.model(**kwargs)
File "C:\Django\test_permissions\lib\site-packages\django\db\models\base.py", line 417, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'author' is an invalid keyword argument for this function
If it is changed to
art1 = Project.objects.create(
rest of code is okay
It works okay. So I guess an error. Maybe, I'm unsure.
Anyway, still cut&paste into shell until I get to
>>> assert user1.has_perm('permission.change_article') == False
Traceback (most recent call last):
File "<console>", line 1, in <module>
AssertionError
So I try
>>> assert user1.has_perm('permission.change_article') == True
Works fine. I have to say at this stage I have no idea as to what is going on.
So next line is
assert user1.has_perm('permission.change_article', art1) == True
And now the traceback
>>> assert user1.has_perm('permission.change_article', art1) == True
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\django\test_permissions\lib\site-packages\django\contrib\auth\models.py", line 336, in has_perm return _user_has_perm(self, perm, obj)
File "c:\django\test_permissions\lib\site-packages\django\contrib\auth\models.py", line 273, in _user_has_perm if backend.has_perm(user, perm, obj):
File "c:\django\test_permissions\lib\site-packages\permission\backends.py", line 71, in has_perm if handler.has_perm(user_obj, perm, obj=obj):
File "c:\django\test_permissions\lib\site-packages\permission\handlers.py", line 237, in has_perm if permission_logic.has_perm(user_obj, perm, obj):
File "c:\django\test_permissions\lib\site-packages\permission\logics\author.py", line 122, in has_perm author = field_lookup(obj, self.field_name)
File "c:\django\test_permissions\lib\site-packages\permission\utils\field_lookup.py", line 42, in field_lookup return field_lookup(field_lookup(obj, field_path[0]), field_path[1])
File "c:\django\test_permissions\lib\site-packages\permission\utils\field_lookup.py", line 41, in field_lookup return getattr(obj, field_path[0])
AttributeError: 'Project' object has no attribute 'project'
Have I done something wrong?
I have no idea what to do. I am as far out as Bishops Rock Lighthouse ;-)
I need to get permissions working for my project. Is this the app to go with?
BTW.
(test_permissions) c:\django\test_permissions\test_permissions>pip freeze
Django==1.6.5
Pillow==2.2.2
South==0.8.4
app-version==0.1.2
django-appconf==0.6
django-crispy-forms==1.4.0
django-permission==0.8.0
six==1.7.0
tolerance==0.1.1
Working in virtualenv on Win7
Tommy.
I'm the author of this library.
I'm really sorry but the example code was mis-leading. If you follow the Note section which said "From django-permission version 0.8.0, you can specify related object with field__name attribute like django queryset lookup. See the working example below:", the Article does not have author that's why django blame your code. It should be something like
prj1 = Project.objects.create(
title="Project 1",
body="hello",
author=user1,
)
art1 = Article.objects.create(
title="Article 1",
body="foobar hogehoge",
project=prj1,
)
Then the example should work.
I'll update the document as soon as possible.