Skip to content

[BUG] ModelSchema does not support ForeignKey as field_id #1560

@asaff1

Description

@asaff1

Describe the bug
A clear and concise description of what the bug is.

Versions (please complete the following information):

  • Python version: 3.11
  • Django version: 4.2.33
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.11.4

Not sure if that intentional, but that doesn't make a lot of sense:

class OrderOut(ModelSchema)

   class Meta:
       model = Order
       fields = [ "user_id"]  # user_id is foreignkey on Order model

Serialization fails with error like:
ninja.errors.ConfigError: DjangoField(s) {'user_id'} are not in model <class 'app.models.Order'>

However if I change the field to user instead of user_id it does work. Why is that? I believe user_id should also work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions