Access fields dynamically in a django serilaizer - django

I am trying to acsess fields of a model dynamically(on the basis of call from frontend) in the serializer but unable to do so
code:
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
print("self", self)
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
class ProductTestSerializer(DynamicFieldsModelSerializer, serializers.ModelSerializer):
class Meta:
model = Product
fields = ("id",)
class ProductTestAPIView(generics.ListAPIView):
def get(self, request):
obj = Product.objects.all()
data = ProductTestSerializer(obj, many=True)
s_data = data.data
return Response(s_data)
URL:
http://127.0.0.1:8000/products-test/?fields=id,short_code
It returns the following error:
KeyError: 'request'
at
fields = self.context['request'].query_params.get('fields')

Pass the request when calling the DynamicFieldsModelSerializer, like this:
serializer = DynamicFieldsModelSerializer(request.data, context={'request': request})

Related

How to use the same ModelForm form different admin classes with different models

I'm trying to create a common form with a date range validation that will be used in 20 admin classes with different model
So I'm creating this mixin for each of them to use
class DateControllerMixin():
def get_queryset(self, request):
qs = dateControlQuerySet(super().get_queryset(request), self.parameter_name, request)
return qs
def get_form(self, request, obj=None, change= None,**kwargs):
print(self.parameter_name)
if request.user.groups.filter(name='slaughterhouse_date_controlled').exists():
form = DateControlledForm
form.model_class = self.model
form.parameter_name = self.parameter_name
return form
return super().get_form(request, obj, change, **kwargs)
And this is the form but I can't find a way to make the form without specifying the model class in the Meta or use class attributes
class DateControlledForm(forms.ModelForm):
def __init__(
self, *args, **kwargs
):
self._meta.model = self.model_class
super(DateControlledForm, self).__init__(*args, **kwargs)
class Meta:
# model = self.model_class
fields='__all__'
widgets = {
'the_date': AdminDateWidget(),
}
def clean(self, **kwargs):
print(self.paramter_name)
date = self.cleaned_data.get('the_date')
today = datetime.today().date()
days_limit = AppConfigurations.objects.get(parameter_name=self.parameter_name).parameter_value
first_day = today - timedelta(days = int(days_limit))
if date < first_day:
raise forms.ValidationError({
'the_date': [_(f"Date cannot be before {first_day}.")]})
return self.cleaned_data
I tried to edit the meta options from the init method but it didn't work

Django REST Framework: Does ModelSerializer have an option to change the fields dynamically by GET or (POST, PUT, DELETE)?

Does ModelSerializer have an option to change the fields dynamically by GET or (POST, PUT, DELETE)?
While GET requires complex fields such as nested serializers, these are not required for (POST, PUT, DELETE).
I think the solution is to use separate serializers for GET and (POST, PUT, DELETE).
But in that case, I'd have to create quite a few useless serializers.
Is there any good solution?
class PlaylistSerializer(serializers.ModelSerializer):
user = UserDetailSerializer(read_only=True)
tracks = serializers.SerializerMethodField()
is_owner = serializers.SerializerMethodField()
is_added = serializers.SerializerMethodField()
is_favorited = serializers.BooleanField()
class Meta:
model = Playlist
fields = (
"pk",
"user",
"title",
"views",
"is_public",
"is_wl",
"created_at",
"updated_at",
"tracks",
"is_owner",
"is_added",
"is_favorited",
)
def get_is_owner(self, obj):
return obj.user == self.context["request"].user
def get_tracks(self, obj):
queryset = obj.track_set
if queryset.exists():
tracks = TrackSerializer(queryset, context=self.context, many=True).data
return tracks
else:
return []
def get_is_added(self, obj):
try:
return obj.is_added
except AttributeError:
return False
class PlaylistUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Playlist
fields = ("title", "is_public")
first you need to create a class and inherit your serializer from this class as below:
from rest_framework import serializers
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""To be used alongside DRF's serializers.ModelSerializer"""
#classmethod
def default_fieldset(cls):
return cls.Meta.fields
def __init__(self, *args, **kwargs):
self.requested_fields = self._extract_fieldset(**kwargs)
# Fields should be popped otherwise next line complains about
unexpected kwarg
kwargs.pop('fields', None)
super().__init__(*args, **kwargs)
self._limit_fields(self.requested_fields)
def _extract_fieldset(self, **kwargs):
requested_fields = kwargs.pop('fields', None)
if requested_fields is not None:
return requested_fields
context = kwargs.pop('context', None)
if context is None:
return None
return context.get('fields')
def _limit_fields(self, allowed_fields=None):
if allowed_fields is None:
to_exclude = set(self.fields.keys()) - set(self.default_fieldset())
else:
to_exclude = set(self.fields.keys()) - set(allowed_fields)
for field_name in to_exclude or []:
self.fields.pop(field_name)
#classmethod
def all_fields_minus(cls, *removed_fields):
return set(cls.Meta.fields) - set(removed_fields)
then your serializer would be something like this:
class PlaylistSerializer(DynamicFieldsModelSerializer):
class Meta:
model = Playlist
fields = ("pk", "user", "title", "views", "is_public",
"is_wl", "created_at", "updated_at", "tracks", "is_owner",
"is_added", "is_favorited",)
#classmethod
def update_serializer(cls):
return ("title", "is_public")
#classmethod
def view_serializer(cls):
return ("title", "is_public", "is_owner", "is_added")
then you will call your serializer as below:
PlaylistSerializer(instance, fields=PlaylistSerializer.update_serializer()).data

Django Rest Framework: using DynamicFieldsModelSerializer for excluding serializer fields

I have a serializer that is used in a couple of endpoints (generics.ListAPIView), but in one of them I need to hide a serializer field. I would prefer to not write a new serializer just to cover this case.
Starting from DRF 3.0 we have dynamic fields for serializers (https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields), but I'm having some troubles to fully understand how to use them.
I created this serializer:
class TestSerializer(DynamicFieldsModelSerializer):
user_req = UserSerializer(read_only=True)
user_tar = UserSerializer(read_only=True)
class Meta:
model = TestAssoc
fields = ("user_req", "user_tar")
and this is my endpoint:
class TestEndpointListAPIView(generics.ListAPIView):
serializer_class = TestSerializer
permission_classes = [IsAuthenticated]
lookup_field = 'test_username'
def get_queryset(self):
return ...
Now I need to hide the 'user_tar' field from the output, and according to the documentation I should instantiate the serializer with something like:
TestSerializer(fields=('user_req'))
but how should I do this inside my TestEndpointListAPIView? Should I override get_serializer?
Thanks for the help
EDIT:
I've found the following solution, by overriding the get_serialized function:
def get_serializer(self, *args, **kwargs):
serializer_class = self.get_serializer_class()
kwargs['context'] = self.get_serializer_context()
kwargs['fields'] = ['user_req']
return serializer_class(*args, **kwargs)
I'd like to know if it is a good solution. Thanks!
Add this piece of code to __init__ method of the serializer class as suggested in the DRF docs:
class TestSerializer(serializers.ModelSerializer):
user_req = UserSerializer(read_only=True)
user_tar = UserSerializer(read_only=True)
class Meta:
model = TestAssoc
fields = ("user_req", "user_tar")
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
fields = kwargs.pop('fields', None)
# Instantiate the superclass normally
super(TestSerializer, self).__init__(*args, **kwargs)
if fields is not None:
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields)
for field_name in existing - allowed:
self.fields.pop(field_name)
And so when you call the serializer from views.py do this :
TestSerializer(queryset, fields=('user_req'))
Alternatively what you can do is define a class
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
fields = kwargs.pop('fields', None)
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
if fields is not None:
allowed = set(fields)
existing = set(self.fields)
for field_name in existing - allowed:
self.fields.pop(field_name)
Now import this class if you have defined it in some other file and then inherit it using
class TestSerializer(DynamicFieldsModelSerializer):
This way:
class TestSerializer(DynamicFieldsModelSerializer):
user_req = UserSerializer(read_only=True)
user_tar = UserSerializer(read_only=True)
class Meta:
model = TestAssoc
fields = ("user_req", "user_tar")
Now you can do
TestSerializer(queryset, fields=('user_req'))
Update
In the views. Take an example of ListAPIView
class DemoView(ListAPIView):
queryset = TestAssoc.objects.all()
def get(self, request):
try:
queryset = self.get_queryset()
data = TestSerializer(queryset, fields=('user_req')).data
return Response( {"data" : data } ,status=status.HTTP_200_OK)
except Exception as error:
return Response( { "error" : str(error) } , status=status.HTTP_500_INTERNAL_SERVER_ERROR)

AttributeError - object has no attribute 'create'

I'm trying to save a through model which has the following attributes via Django-rest-framework
when sending a POST (I'm trying to create a new instance), I get the following error:
AttributeError at /api/organisation/provider/
'EnabledExternalProvider' object has no attribute 'create'
any ideas as to what i'm doing incorrectly?
the through model in question is:
class EnabledExternalProvider(models.Model):
provider = models.ForeignKey(ExternalProvider, related_name='associatedProvider')
organisation = models.ForeignKey(Organisation, related_name='associatedOrg')
enabled = models.BooleanField(default=True)
tenantIdentifier = models.CharField('Tenant identifer for organisation', max_length = 128, null=True, blank=True)
def __str__(self):
return self.provider + '-' + self.organisation
my view is:
class EnabledExternalProvider(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):
serializer_class = ConnectedServiceSerializer
def get_queryset(self):
return EnabledExternalProvider.objects.filter(organisation=self.request.user.organisation_id)
def get_object(self):
queryset = self.filter_queryset(self.get_queryset())
# make sure to catch 404's below
obj = queryset.get(organisation=self.request.user.organisation_id)
self.check_object_permissions(self.request, obj)
return obj
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
and my serializer is:
class ConnectedServiceSerializer(serializers.ModelSerializer):
provider=ExternalProviderSerializer(read_only=True)
organisation=OrganisationDetailSerializer(read_only=True)
class Meta:
model = EnabledExternalProvider
fields = ( 'organisation', 'enabled', 'tenantIdentifier')
read_only_fields = ('organisation', 'provider')
I'm POSTing the following:
{"provider":"1","tenantIdentifier":"9f0e40fe-3d6d-4172-9015-4298684a9ad2","enabled":true}
Your view doesn't have that method because you haven't defined it, or inherited from a class that has it; your mixins provide retrieve and update, but not create.
You could add mixins.CreateModelMixin to the inheritance, but at this point you should really be using a ViewSet instead.

set variable into queryset of forms.py from my generic view or url

I want to set a dynamic variable into queryset of forms.py , I used __init__ to pass the dynamic variable , I think the code in forms.py is correct, the problem is how to pass the variable in views?
forms.py :
class ContainerForm(forms.ModelForm):
vehicle=forms.ModelChoiceField(required=False,queryset=Vehicle.objects.all(),widget=forms.Select(attrs={'class':'form-control'}))
def __init__(self, *args, **kwargs):
vehicle_id = kwargs.pop('vehicle_id',None)
super(ContainerForm, self).__init__(*args, **kwargs)
if vehicle_id:
self.fields['vehicle'].queryset = Vehicle.objects.filter(id=vehicle_id)
views.py
class ContainerCreate(CreateView):
form_class = ContainerForm(id= vehicle_id)
template_name = 'vehicule_app/container_form.html'
the error said :
Exception Value:'ContainerForm' object is not callable
If you want to use the vehicle_id from the URL, then you can exclude the field from the model form:
class ContainerForm(forms.ModelForm):
class Meta:
model = Container
exclude = ['vehicle']
You can then fetch the parameter from self.kwargs, and set the value on the form's instance in get_form_kwargs:
class ContainerCreate(CreateView):
form_class = ContainerForm
template_name = 'vehicule_app/container_form.html'
def get_form_kwargs(self):
kwargs = super(ContainerCreate, self).get_form_kwargs()
if kwargs['instance'] is None:
kwargs['instance'] = Container()
kwargs['instance'].vehicle_id = self.kwargs['pk'] # Fetch the vehicle_id from the URL
return kwargs
Note that the above code will not validate the id from the URL. The user could change it to any value they like.
If you want to keep the vehicle field in the form but with a single choice, then override the __init__ method and set the queryset.
class ContainerForm(forms.ModelForm):
class Meta:
model = Container
def __init__(self, *args, **kwargs):
vehicle_id = kwargs.pop('vehicle_id')
self.fields['vehicle'].queryset = Vehicle.objects.filter(id=vehicle_id)
Then in the get_form_kwargs method, add vehicle_id to kwargs instead:
class ContainerCreate(CreateView):
form_class = ContainerForm
template_name = 'vehicule_app/container_form.html'
def get_form_kwargs(self):
kwargs = super(ContainerCreate, self).get_form_kwargs()
kwargs['vehicle_id'] = self.kwargs['pk']
return kwargs