Automated related ForeignKey Models in ModelSchema
See original GitHub issueCan related models be handled automatically?
Model
class ExampleModel(models.Model):
relation = models.ForeignKey(SomeOtherModel, on_delete=models.PROTECT, related_name='example')
ModelSchema
class ExampleSchema(ModelSchema):
class Config:
model = models.ExampleModel
model_fields = ['id', 'relation']
API
@api.post('/example')
def create_example(request, payload: schemas.ExampleSchema):
pl = payload.dict()
relation = get_object_or_404(SomeOtherModel, id=payload.relation)
pl['relation'] = relation
example = models.ExampleModel.objects.create(**pl)
return {'id': example.id}
The above example works with the payload {'relation_id': 4}
if the object with ID 4 exists.
Question 1
Is there any specific reason why the field has to be named relation_id
in the payload and not just relation
?
Question 2 In the API, is it really necessary to do
pl = payload.dict()
relation = get_object_or_404(SomeOtherModel, id=payload.relation)
pl['relation'] = relation
or can that be somehow done automatically?
It seems that writing the get_object_or_404()
is currently needed, or can that be omitted with an other apporach?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Using South to convert ForeignKey TO ManyToManyField not ...
I am using South to change a ForeignKey TO ManyToManyField in one of the models in Django but it is not working out...
Read more >Fluent → Schema - Vapor Docs
Foreign key actions happen solely in the database, bypassing Fluent. This means things like model middleware and soft-delete may not work correctly. Dictionary¶....
Read more >DbSchema Features
DbSchema model contains its own image of the schema, independent from the database. Connecting to another database won't change the model schema unless...
Read more >Models — Tortoise ORM v0.17.3 Documentation
To get working with models, first you should import them ... that we require that direct filters be applied to the DB-backing field...
Read more >Defining a Schema - Django Ninja
Imagine we have a Task Django model with a User ForeignKey: from django.db import models class Task(models.Model): title = models.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I´m using that workaround but it gets this when I try to post:
ValueError: Cannot assign "18": "Activity.town" must be a "Town" instance.
My code:
URLS.PY
The data Im trying to post
{icon_id: '6', name: 'mn bnb', highlight: false, town_id: 18}
SCHEMAS.PY
MODELS.PY
@fantasticle
I think it should not - can you give your example ?
Maybe workaround for you case would be to set FK ids as attributes: