Skip to content
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

Handling GenericForeignKey's #32

Open
AndreiPashkin opened this issue Feb 2, 2014 · 3 comments
Open

Handling GenericForeignKey's #32

AndreiPashkin opened this issue Feb 2, 2014 · 3 comments

Comments

@AndreiPashkin
Copy link
Contributor

def get_generator(self, field):
    if field.name == 'content_type':
            return generators.ChoicesGenerator(
                   values=[ContentType.objects.get_for_model(m)
                           for m in [ModelOne, ModelTwo, ModelThree]])
    elif field.name == 'object_id':
        # where to pick right model class? ¯\(°_o)/¯

I think, for that case, instances of GeneridForeignKey are also could be passed to get_generator(), and being skipped in base implementation, so user will can handle them in subclass method.
Or another hook like prost_process_instance could be defined, which must be called before instance being saved first time.

@AndreiPashkin
Copy link
Contributor Author

Another issue with GFK's is that you cant just do autofixture.creante_one(SomethingWithGFK, field_values={'content_object': some_object}) content_type will be set to random ContentType and object_id to random value, so content_object will refer to unexistent object.

@aschriner
Copy link
Contributor

I took a crack at implementing this.
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.

@alb3rto269
Copy link

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

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

No branches or pull requests

3 participants