I am exposing an API for a particular model, and want to serialize some of its related fields. These related fields are commonly repeated, and I don't want to have to do a numerous db queries for each related field serialization. Is there a simple way to pre-query all related instances, and then have have the RelatedField serializer look it up in a dictionary? Or maybe to specify from the ModelSerializer of the related field?
You can use Django's standard prefetch_related and select_related methods on your queryset.
On the view, use the queryset attribute, rather than the model shortcut.
For example...
class ExampleView(generics.ListCreateAPIView):
serializer_class = ExampleSerializer
queryset = Example.objects.select_related(...)
Related
I am trying to create a script that could help me generate a GraphQL schema with all fields and relations exist in the models.
if I could get that, php-lighthouse would help me for the rest of the work.
There is no way to get all relations of a particular Eloquent model without the user specifying them in some way, such as an attribute on the model that holds all relation names, or type-hinting those methods returning a relation. See laravel-eloquent-to-graphql for an example.
You can however get all fields of a model (that are saved in the database) by inspecting its table.
If you're looking for a GraphQL generator, there are Laravel Bakery and laravel-eloquent-to-graphql. Laravel Bakery generates a GraphQL schema on-the-fly, while laravel-eloquent-to-graphql generates a GraphQL schema statically.
There is also Lighthouse-Utils, which can generate queries and mutations for already-defined types.
Can I extract a specific column for a model instance in Laravel instead of using query methods statically? more clearly, instead of writing the code below:
MyModel::select('columnName')->where('id', $model->id)->first();
can we write?
$model->select('columnName');
Well, when you grab it from a model class, it is a Eloquent Object, and it has all attributes from the database.
But if you are talking about serializing that into an array or JSON everytime, then you need to modify your $visible array ;
if you wanna transform it for a specific case, then you need to do :
MyModel::select('columnName')->where('id', $model->id)->first()->pluck('columnName');
I would like to hide for example the id field but NOT by annotating the field itself using #ApiModelProperty but at the ParameterMapper object level. That is, annotating ParameterMapper, since this object is used in multiple places and I am not showing the same fields in all place of the swagger doc.
is it possible to use a relational field of the own model i've worked?
For example
_name = 'x.x'
x_ids = fields.Many2many('x.x')
I need to show the records I've created of the same model in relation field, any suggestions?
That is entirely possible, and you can use relational ways exactly the same way then you would do it with an another model.
Basically, required fields for viewset are based on models.py file. If I use create method and it is defined blank=True and null=True in models.py then this field is not required.
But how to define that this field is not required in models.py but required in viewset.py?
Actually, it will be required by the serializer by setting this field's required to True.
If you don't want to explicitly define the field, you may take advantage of the serializer's extra_kwags