django-autofixture icon indicating copy to clipboard operation
django-autofixture copied to clipboard

Add support for GenericFK fields

Open aschriner opened this issue 10 years ago • 7 comments

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

aschriner avatar Apr 07 '14 13:04 aschriner

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.

aschriner avatar Apr 07 '14 13:04 aschriner

Soooo great! Thanks for this awesome pull request. I would say the tests for GenericFKSelector go into autofixture_tests/tests/generator.py were as the tests for the changes on the AutoFixture class go into autofixture_tests/tests/base.py. But feel free to create a new file if you think this should go somewhere else.

Cheers! :)

gregmuellegger avatar Apr 07 '14 22:04 gregmuellegger

I think theres simpler solution exists, without monkeypatching etc. What I dont understand is the Link class - how it works? I think it must be used for handling nested GFK relations. Greg, can you make a commit with dtailed comments for this class and its methods?

AndreiPashkin avatar May 14 '14 09:05 AndreiPashkin

Gosh, that is code which is over 4 years old and that I wrote in the middle of the night at that time :sweat_smile:

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.

gregmuellegger avatar May 30 '14 08:05 gregmuellegger

I think that Link must not know anything about nested relations and represent and generalize only one relation, with a parameters such as one/many, taget content-type, target object-id (i think its enough to cover all types of relations). Nested relations can be handled by some clever recursive function (beware of inifinite recursion!), which will be invoked during fixture creation process when AutoFixture finds any relation.

AndreiPashkin avatar May 30 '14 09:05 AndreiPashkin

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.

m000 avatar Sep 12 '16 15:09 m000

Here is how I'm using AutoFixture in models with GenericForeignKey. Feel free to use and/or modify it.

https://gist.github.com/alb3rto269/def529c94f47272bed8d2990c93d99f2

alb3rto269 avatar Mar 23 '18 20:03 alb3rto269