-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add support for GenericFK fields #38
base: master
Are you sure you want to change the base?
Conversation
Overall method: find GenericFk fields from model._meta.virtual_fields, add them to the list of fields to be processed, and remove the constituent content_type and object_id fields from the processing list. Then use the GenericFK generator to either select or create an object to assign.
Needs tests. Looks like you've got a big test suite - if you point me to where the tests should fit into the test suite I will write them. |
@@ -230,6 +235,17 @@ def add_constraint(self, constraint): | |||
''' | |||
self.constraints.append(constraint) | |||
|
|||
def _normalize_genericfk_field(self, field): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this method monkey patch the actual field object that comes from the model? I think it might be better to not do that and instead return a copy or a mock of the actual field. That way we don't create sideeffects for any other code that uses the model. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you are right. good catch - i will change that function to return a copy of the field object.
Soooo great! Thanks for this awesome pull request. Cheers! :) |
_normalize_genericfk_field() now operates on a copy of the original field instead of the original field object
@@ -237,6 +254,9 @@ def get_generator(self, field): | |||
specified field will be ignored (e.g. if no matching generator was | |||
found). | |||
''' | |||
if isinstance(field, GenericForeignKey): | |||
field = self._normalize_genericfk_field(field) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return generator right here?
I think theres simpler solution exists, without monkeypatching etc. |
Gosh, that is code which is over 4 years old and that I wrote in the middle of the night at that time 😅 I just made an attempt to document but changed my mind and will try to make my best to refactor this one out and replace it with something that's easier to understand... I think that's easier and more future proof. |
I think that |
Are there plans to add this feature? In my gfk branch, I've merged the PR and resolved the conflicts and a couple of minor issues. |
Here is how I'm using https://gist.github.com/alb3rto269/def529c94f47272bed8d2990c93d99f2 |
Added generator for GenericFK relations.
Overall method: find GenericFk fields from model._meta.virtual_fields,
add them to the list of fields to be processed, and remove the
constituent content_type and object_id fields from the processing list.
Then use the GenericFK generator to either select or create an
object to assign.
Addresses #32