-
-
Notifications
You must be signed in to change notification settings - Fork 452
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] ForeignKey field in modelSchema do not use the related alias_generator #828
Comments
Hi @stvdrsch See #811 - you need to add @api.get('/dealers', response=list[DealerSchema], by_alias=True)
def dealers(request):
return Dealer.objects.all() to make this behaviour by default you can extend the Router and add by_alias as default: from ninja import Router
class MyRouter(Router):
def add_api_operation(self, *a, **kw):
kw['by_alias'] = True
return super().add_api_operation(*a, **kw)
...
router = MyRouter()
@router.get('/dealers', response=list[DealerSchema])
def dealers(request):
return Dealer.objects.all() |
Yes, that was added and the In the response in the initial question fields like |
I think I have a solution for this. Let's say we have a django model and ninja schema like so class Book(models.Model):
id = models.AutoField(primary_key=True)
full_name = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
class BookSchema(ModelSchema):
model_config = dict(alias_generator=to_camel, populate_by_name=True)
class Meta:
model = Book
fields = ["id", "full_name", "author"] The ModelSchema metaclass factory currently creates fields for ForeignKeys with aliases that correspond with their Django field attribute name ( Instead, we could set the property names on the Pydantic model to match these field attribute names, so This causes a new problem where users can no longer access the property by the field name as they would expect since it has been renamed ( The final generated class would be equivalent to: class BookSchema(Schema):
model_config = dict(alias_generator=to_camel, populate_by_name=True)
id: int = Field(...)
full_name: str = Field(...) # generated alias "fullName"
author_id: int = Field(...) # generated alias "authorId"
@property
def author(self):
return self.author_id
@author.setter
def author(self, value):
self.author_id = value How does this sound @vitalik ? If you're OK with this solution I can write a PR for it. |
Describe the bug
When using an alias_generator in the config of a
modelSchema
the id's returned for ForeignKey Fields do not use that generatorVersions (please complete the following information):
I have this
Dealer
modelWhich adheres to this modelschema
which uses this schema that converts properties to camelcase
All of this works for the fields directly attached to the instances, but foreignkey fields (that end in
_id
) don't seem to be converted. Would it be possible to have the generator adjust ALL fields?E.g. this is the schema of the response of a dealer instance
distributor_id
is still not camelcased although the rest of the keys are. Thedistributor_id
field is not sent through the alias_generator. Applying the same alias generator to the modelschema of the distributor also doesn't fix the issue.I would guess this is a bug with the framework? Or potentially it is expected to behave like this...
The text was updated successfully, but these errors were encountered: