Djangorest framework. Required field only in viewset - django-rest-framework

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

Related

Is there another approach or io.swagger annotation that hides fields like how #ApiModelProperty does to hide fields of a custom Object?

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.

How can the primary key be specified when creating a new model instance in Django Rest Framework?

I'm using Django Simple History to store information about who edited what. In order to conform to the adage, "Never Use a Warning When You Mean Undo", I am providing users an un-delete option after they do a delete action. When they do the delete, I do the actual HTTP DELETE request to DRF. When the user clicks un-delete, I need to recreate the object. In order to ease the maintenance of the history of the object, I want to restore the object with the exact same primary key.
Right now, my HyperLinkedModelSerializer has both the id and url fields of the object, but specifying the id when posting does not create the object with that id, despite the fact that the id is available.
How can I specify the id/primary key when creating an object in DRF?
This is happening because HyperlinkedModelSerializer does not include the id field by default.
From the DRF docs:
By default the serializer will include a url field instead of a
primary key field.
You will have to explicitly define the id field in the serializer.
class MySerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField() # explicitly define the 'id' field
...

Parse generic foreign key

I need to store a reference to a parse model in an external service.
In my code I however just have a model instance, not the model class.
How do i get both an identifier for the model type and the model instance's id?
You could generate a composite key based on the class name and object ID. Eg, User-JFUB234DF. Just choose a separator that's an invalid character in a classname to avoid conflicts with actual classnames. You'd need to parse the key to run any lookup to split out the class name and object ID.

Is there a way to pre-lookup related field models?

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(...)

how to validate custom fields in phpfox

i have created few custom fields in registration form .
And i want to validate those field .
Any help will be apriciated.
Thanks
You can validate custom fields from admin where you create custom fields
there an option to set them required or not required.

Resources