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

Related managers not created properly if related model generated after parent model #58

Open
rvinzent opened this issue Jul 3, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@rvinzent
Copy link
Owner

rvinzent commented Jul 3, 2022

Steps to Reproduce

model_schema = ModelSchema.objects.create(name="SimpleModel")
related_model_schema = ModelSchema.objects.create(name="RelatedModel")

FieldSchema.objects.create(
    name="related",
    model_schema=model_schema,
    class_name="django.db.models.ForeignKey",
        kwargs={
            "to": another_model_schema.model_name,
            "on_delete": models.CASCADE,
            "related_name": "parent_objects",
        },
)
model = model_schema.as_model()
related_model = related_model_schema.as_model()

related_instance = related_model.objects.create()
model_instance = model.objects.create(related=related_instance)
related_instance.parent_objects

AttributeError: "parent_objects" does not exist

Workaround

Call related_model_schema.as_model() before `model_schema.as_model(). This works for some reason

@rvinzent rvinzent added the bug Something isn't working label Jul 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant