Skip to content

Add support for GenericRelations #319

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

Merged
merged 7 commits into from
Feb 23, 2017
Merged

Add support for GenericRelations #319

merged 7 commits into from
Feb 23, 2017

Conversation

santiavenda2
Copy link
Contributor

@santiavenda2 santiavenda2 commented Feb 1, 2017

Fixed #224

Also, we fixed

ConftestImportFailure: (local('/home/santiago/develop/invgate-django-rest-framework-json-api/example/tests/conftest.py'), (<type 'exceptions.ImportError'>, ImportError('No module named faker',), <traceback object at 0x7f64f3d8c5a8>))

Fake-factory has been deprecated, changing its name to Faker

@santiavenda2 santiavenda2 changed the title Change fake-factory (deprecated) requirement to Faker Add support for GenericRelations Feb 1, 2017
@@ -58,3 +58,11 @@ class Meta:
body = factory.LazyAttribute(lambda x: faker.text())
author = factory.SubFactory(AuthorFactory)


class EntryTaggedItemFactory(factory.django.DjangoModelFactory):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you call this TaggedItemFactory instead of EntryTaggedItemFactory? That seems more in line with the other factory naming conventions here.

@@ -3,6 +3,9 @@

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.contrib.contenttypes.models import ContentType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing this package's code, imports are usually alphabetically sorted. Could you move these contrib imports about the db import?

@@ -30,11 +30,14 @@
if django.VERSION >= (1, 9):
from django.db.models.fields.related_descriptors import ManyToManyDescriptor, ReverseManyToOneDescriptor
ReverseManyRelatedObjectsDescriptor = type(None)
from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method of having two mirrored imports that set one to the None type is something I found confusing. After digging through the Django contenttypes source, I see that what is happening is trying to deal with a rename of the descriptor from 1.8 to 1.9. Unfortunately, this method leads to extra branches in an already large if/elif section.

How about using an import alias instead?

if django.VERSION >= (1, 9):
    # ... snip unmodified lines ...
    from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor
else:
    from django.contrib.contenttypes.fields import ReverseGenericRelatedObjectsDescriptor as ReverseGenericManyToOneDescriptor

I believe this would clean up the code, be more efficient, and capture the fact that the descriptor was renamed. It seems this pattern was already followed for the first two imports that follow the else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -2,6 +2,6 @@
pytest>=2.9.0,<3.0
pytest-django
pytest-factoryboy
fake-factory
Faker
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up this package name change. This change has bit me on a few projects already.

@mblayman
Copy link
Collaborator

Thanks @santiavenda2 for the contribution! Making this work for GenericRelations is a feature that I'm sure many users will value considering the popularity of the contenttypes system.

This feels like a feature worth documenting. Would you be able to do some of that? At the very least, I think starting a v2.X entry with a bullet point about GenericRelations should happen.

I'd be happy to merge after some minor modifications occur. 👍

@codecov-io
Copy link

codecov-io commented Feb 23, 2017

Codecov Report

Merging #319 into develop will increase coverage by 0.06%.
The diff coverage is 89.58%.

@@             Coverage Diff             @@
##           develop     #319      +/-   ##
===========================================
+ Coverage    91.58%   91.64%   +0.06%     
===========================================
  Files           49       50       +1     
  Lines         2318     2359      +41     
===========================================
+ Hits          2123     2162      +39     
- Misses         195      197       +2
Impacted Files Coverage Δ
example/tests/integration/test_meta.py 100% <ø> (ø)
.../tests/integration/test_non_paginated_responses.py 100% <ø> (ø)
example/tests/integration/test_pagination.py 100% <ø> (ø)
example/migrations/0002_taggeditem.py 100% <100%> (ø)
example/serializers.py 100% <100%> (ø)
example/factories/init.py 97.67% <100%> (+0.3%)
example/tests/conftest.py 100% <100%> (ø)
rest_framework_json_api/utils.py 89.75% <60%> (-1.39%)
example/models.py 94.82% <91.66%> (-0.83%)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a19f445...f9bfcc9. Read the comment docs.

@mblayman
Copy link
Collaborator

Nice work. Thanks, @santiavenda2!

@mblayman mblayman merged commit fbe49a1 into django-json-api:develop Feb 23, 2017
amw added a commit to amw/django-rest-framework-json-api that referenced this pull request May 7, 2017
PR django-json-api#319 brought support for generic relations. Unfortunately apps that
don't add contenttypes to it's INSTALLED_APPS would crash and burn:

```
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
```

Also using `type(something) is object()` comparison as a safer
alternative to `type(something) is type(None)`. If `something` happened
to be `None` we would enter a branch that was never supposed to run.
amw added a commit to amw/django-rest-framework-json-api that referenced this pull request May 7, 2017
PR django-json-api#319 brought support for generic relations. Unfortunately apps that
don't add contenttypes to it's INSTALLED_APPS would crash and burn:

```
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
```

Also using `type(something) is object()` comparison as a safer
alternative to `type(something) is type(None)`. If `something` happened
to be `None` we would enter a branch that was never supposed to run.
mblayman pushed a commit that referenced this pull request May 7, 2017
PR #319 brought support for generic relations. Unfortunately apps that
don't add contenttypes to it's INSTALLED_APPS would crash and burn:

```
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
```

Also using `type(something) is object()` comparison as a safer
alternative to `type(something) is type(None)`. If `something` happened
to be `None` we would enter a branch that was never supposed to run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants