tkhyn / django-gm2m

MIT License
36 stars 23 forks source link

Django 1.8 & Factory-Boy #7

Closed tkhyn closed 9 years ago

tkhyn commented 9 years ago

Original report by Paul Collins (Bitbucket: paulcollinsiii, GitHub: paulcollinsiii).


While running my tests, I'm getting a new traceback

Traceback (most recent call last):
  File "/Users/paulcollins/workspace/datanav/backend/tests/functional/test_wholesaler/test_basic.py", line 18, in test_wholesaler_invalid_mgmt
    factories.WholesalerFactory(management=['asdf', 'fdsa'])
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 81, in __call__
    return cls.create(**kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 558, in create
    attrs = cls.attributes(create=True, extra=kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 400, in attributes
    force_sequence=force_sequence,
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/containers.py", line 228, in build
    return stub.__fill__()
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/containers.py", line 83, in __fill__
    res[attr] = getattr(self, attr)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/containers.py", line 105, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/containers.py", line 160, in evaluate
    containers=containers,
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/declarations.py", line 298, in evaluate
    return self.generate(sequence, obj, create, defaults)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/declarations.py", line 385, in generate
    return subfactory.simple_generate(create, **params)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 645, in simple_generate
    return cls.generate(strategy, **kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 612, in generate
    return action(**kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 559, in create
    return cls._generate(True, attrs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 484, in _generate
    obj = cls._prepare(create, **attrs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/base.py", line 459, in _prepare
    return cls._create(model_class, *args, **kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/factory/django.py", line 149, in _create
    return manager.create(*args, **kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/django/db/models/query.py", line 346, in create
    obj = self.model(**kwargs)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/django/db/models/base.py", line 429, in __init__
    if (field.attname not in kwargs and
AttributeError: 'GM2MRelation' object has no attribute 'attname'

This is with 168:5254d90a4dbc from the fix to #5

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Well, it looks like 0.2.6 will have to wait a little bit.

What if you apply this patch:

diff -r cf6858a6e354 gm2m/relations.py
--- a/gm2m/relations.py Thu Apr 23 15:53:12 2015 +1200
+++ b/gm2m/relations.py Fri Apr 24 13:56:23 2015 +1200
@@ -36,6 +36,7 @@
     Each related model has a GM2MRelation towards the source model
     """

+    one_to_many = True
     concrete = False
     generate_reverse_relation = False  # only used in Django 1.7
     related_accessor_class = GM2MRelatedDescriptor
tkhyn commented 9 years ago

Original comment by Paul Collins (Bitbucket: paulcollinsiii, GitHub: paulcollinsiii).


That improves things for sure, but now I'm running into

Traceback (most recent call last):
  File "/Users/paulcollins/workspace/datanav/backend/tests/functional/test_entity/test_api.py", line 182, in test_add_phone_other
    serializer.data['phone_other'].append(model_to_dict(phone))
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/serializers.py", line 466, in data
    ret = super(Serializer, self).data
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/serializers.py", line 213, in data
    self._data = self.to_representation(self.instance)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/serializers.py", line 422, in to_representation
    fields = [field for field in self.fields.values() if not field.write_only]
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/serializers.py", line 313, in fields
    for key, value in self.get_fields().items():
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/serializers.py", line 836, in get_fields
    info = model_meta.get_field_info(model)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/utils/model_meta.py", line 68, in get_field_info
    reverse_relations = _get_reverse_relationships(opts)
  File "/opt/boxen/data/virtualenvs/di_py34/lib/python3.4/site-packages/rest_framework/utils/model_meta.py", line 135, in _get_reverse_relationships
    to_many=relation.field.rel.multiple,
AttributeError: 'GM2MRel' object has no attribute 'multiple'

This is with DRF 3.1.1. I'm not positive this is GM2M related so I'll dig into this more today and report back.

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


It's almost certainly gm2m related, as the multiple attribute is expected by Django internals for the object that is accessible as the rel attribute of a RelatedField, and gm2m hasn't been told to set this attribute yet.

You can try adding the line below:

#!

diff -r cf6858a6e354 gm2m/relations.py
--- a/gm2m/relations.py Thu Apr 23 15:53:12 2015 +1200
+++ b/gm2m/relations.py Sat Apr 25 16:23:15 2015 +1200
@@ -20,6 +20,7 @@

 # default relation attributes
 REL_ATTRS = {
+    'multiple': True,
     'related_name': None,
     'related_query_name': None,
     'through': None,
tkhyn commented 9 years ago

Original comment by Paul Collins (Bitbucket: paulcollinsiii, GitHub: paulcollinsiii).


That appears to resolve that particular set of test failures for me. I'm working through the rest of my test suite still. Several other upgrade problems that aren't related to gm2m. At this point I'm pretty sure you're good to release 0.2.6

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


[re #7] Adds failing test for #7

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


[fix #7] Sets field flags on GM2MRelation

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


[re #7] Adds some missing default attributes for GM2MRel / GM2MUnitRel

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


All done, 0.2.6 has just been released.

Thanks again for your reports.