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

[BUG] Not able to use abstract model to generate an inheritable schema: AttributeError: 'str' object has no attribute '_meta' #900

Closed
Bishwas-py opened this issue Nov 2, 2023 · 3 comments

Comments

@Bishwas-py
Copy link

Bishwas-py commented Nov 2, 2023

I wanted to generate an inheritable model schema for an abstract model ContentGenericModel, which seems to be not possible at the moment.

class ContentGenericModel(WordPressActionsModel, GenericModel, OpenAIModel, ContentGenerationModel):
    title = models.CharField(max_length=11000)
    user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    slug = models.SlugField(max_length=11000, blank=True, null=True)
    uuid = models.UUIDField(editable=False, default=uuid.uuid4)
    ai_title = models.CharField(max_length=11000, blank=True, null=True)
    # .... other fields

    class Meta:
        abstract = True
class WriterContentSchema(ModelSchema):
    wordpress_post: WordPressContentSchema | None
    credits_consumed: list[CreditUsageSchema]
    sub_keywords: list[KeywordSchema]
    associated_wp_site: WordPressSiteSchema | None

    class Config:
        model = ContentGenericModel
        model_fields = [
            "uuid",
            "title",
            "ai_title",
            "newly_created",
            "html_text",
            # other fields
        ]

I am getting following issues:

  File "/home/codie/Projects/blogstorm.ai/backend_blogstorm/writer/schemas/__init__.py", line 15, in <module>
    class WriterContentSchema(ModelSchema):
  File "/home/codie/Projects/blogstorm.ai/backend_blogstorm/venv/lib/python3.11/site-packages/ninja/orm/metaclass.py", line 63, in __new__
    model_schema = create_schema(
                   ^^^^^^^^^^^^^^
  File "/home/codie/Projects/blogstorm.ai/backend_blogstorm/venv/lib/python3.11/site-packages/ninja/orm/factory.py", line 59, in create_schema
    python_type, field_info = get_schema_field(fld, depth=depth)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/codie/Projects/blogstorm.ai/backend_blogstorm/venv/lib/python3.11/site-packages/ninja/orm/fields.py", line 118, in get_schema_field
    internal_type = field.related_model._meta.pk.get_internal_type()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_meta'

Note: It works super well when I change the model to something that is not an abstract model and is inherited from ContentGenericModel.

Versions (please complete the following information):

  • Python version: 3.11.2
  • Django version: 4.1.6
  • Django-Ninja version: 0.22.2
  • Pydantic version: 1.10.13
@Bishwas-py
Copy link
Author

Bishwas-py commented Nov 2, 2023

Currently, I am using hand-written manual schema for the purpose of being fulfilled. I am sorry, if that's what we are meant to do, I am okay with that :)

@vitalik
Copy link
Owner

vitalik commented Nov 2, 2023

well the error is due to the fact that your model is abstract (currently it's not supported)

I guess what you can do meanwhile - create a real model inherited from it (but make it managed=False - that way it will not create a table in your db)

@Bishwas-py
Copy link
Author

well the error is due to the fact that your model is abstract (currently it's not supported)

I guess what you can do meanwhile - create a real model inherited from it (but make it managed=False - that way it will not create a table in your db)

I just wrote the required schema. Thanks :)

@vitalik vitalik closed this as completed Apr 4, 2025
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

2 participants