django-serializer

Allowing Edit to editable=False Fields in Django Admin

谁说胖子不能爱 提交于 2019-12-01 00:43:37
DRF will use the editable=False on a field to default the Serializer to read-only . This is a very helpful / safe default that I take advantage of (ie I won't forget to set the Serializer to read-only). That being said once I have set editable=False is there any way to then force the Django admin to allow editing one of those fields? Presumably the admin is a super user and I do want him to be able to change the fields value but fore safety I want the default Serializer logic to be read only. UPDATE I don't actually need to be able to edit the field as much as "set-it" when I create the object

Django JSON custom serializing losing datetime type

回眸只為那壹抹淺笑 提交于 2019-11-30 23:23:38
I'm encoding data fetched from a Django cursor using Django json libraries, however I'm seeing datetimes after deserializing are now unicode type. Simple example: import datetime from django.core.serializers.json import json, DjangoJSONEncoder today = datetime.datetime.now() encoded = json.dumps(today, cls=DjangoJSONEncoder) type(json.loads(encoded)) >> unicode If I'm not mistaken variable types should be respected. Then I thought maybe there was something like a DjangoJSONDecoder, but nothing. what am I doing wrong? is this the expected behavior? It can't work how you think it should. The

Django JSON custom serializing losing datetime type

荒凉一梦 提交于 2019-11-30 17:18:34
问题 I'm encoding data fetched from a Django cursor using Django json libraries, however I'm seeing datetimes after deserializing are now unicode type. Simple example: import datetime from django.core.serializers.json import json, DjangoJSONEncoder today = datetime.datetime.now() encoded = json.dumps(today, cls=DjangoJSONEncoder) type(json.loads(encoded)) >> unicode If I'm not mistaken variable types should be respected. Then I thought maybe there was something like a DjangoJSONDecoder, but

SerializerClass field on Serializer save from primary key

冷暖自知 提交于 2019-11-30 06:53:23
I am working developing an API with Django-rest-framework and consuming it from a web app. It has a Physician Model with a Fk from the django.auth User model. I want to post from a form to the Physician Model but the serializer returns this message: {"user":{"non_field_errors":["Invalid data. Expected a dictionary, but got unicode."]}} I am sending the primary key of the user object. Which is the right (or just one way) to store a foreign key on DRF. I have tried overriding get_validation_exclusions on the serializer and overriding perform_create method on the viewset. The api and the web app

How to paginate queryset for Serializer

你离开我真会死。 提交于 2019-11-29 16:57:38
I'm retreiving Category and its outfits list. My problem is there are too many outfits belong to a category . class CategoryListAPIView(generics.RetrieveAPIView): serializer_class = CategoryDetailSerializer ... class CategoryDetailSerializer(serializers.ModelSerializer): outfits = serializers.SerializerMethodField() ... class Meta: model = Category fields = ( ... 'outfits', ... ) def get_outfits(self, obj): //This is returning 39 items. // Can we paginate this? if obj.outfits is not None: return OutfitListSerializer(obj.outfits, many=True).data return None Can we paginate it so that user can

django serialize foreign key objects

一世执手 提交于 2019-11-29 07:25:40
Serialize django model with foreign key models Serializing Foreign Key objects in Django get foreign key objects in a single query - Django There are couple of question asking for the same thing already. But they are from 2010, and it didn't help me so much. So I figure it maybe been some update to this front since 2010? On google I found this link , which explain usage of natural keys . However my problem concerns of getting foreign objects from django.contrib.auth.models.User so it doesn't help. My problem is as following. I want to serialize the QuerySet so I get the foreign key objects

SerializerClass field on Serializer save from primary key

限于喜欢 提交于 2019-11-29 06:20:53
问题 I am working developing an API with Django-rest-framework and consuming it from a web app. It has a Physician Model with a Fk from the django.auth User model. I want to post from a form to the Physician Model but the serializer returns this message: {"user":{"non_field_errors":["Invalid data. Expected a dictionary, but got unicode."]}} I am sending the primary key of the user object. Which is the right (or just one way) to store a foreign key on DRF. I have tried overriding get_validation

Dynamically exclude or include a field in Django REST framework serializer

可紊 提交于 2019-11-28 22:42:43
I have a serializer in Django REST framework defined as follows: class QuestionSerializer(serializers.Serializer): id = serializers.CharField() question_text = QuestionTextSerializer() topic = TopicSerializer() Now I have two API views that use the above serializer: class QuestionWithTopicView(generics.RetrieveAPIView): # I wish to include all three fields - id, question_text # and topic in this API. serializer_class = QuestionSerializer class QuestionWithoutTopicView(generics.RetrieveAPIView): # I want to exclude topic in this API. serializer_class = ExamHistorySerializer One solution is to

Editing django-rest-framework serializer object before save

旧时模样 提交于 2019-11-28 04:20:42
I want to edit a django-rest-framwork serializer object before it is saved. This is how I currently do it - def upload(request): if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if form.is_valid(): # All validation rules pass obj = form.save(commit=False) obj.user_id = 15 obj.save() How can I do it with a django-rest-framework serializer object? @api_view(['POST','GET']) def upload_serializers(request): if request.method == 'POST': serializer = FilesSerializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): serializer.save() zephyr You can edit the

Django rest framework serializing many to many field

瘦欲@ 提交于 2019-11-27 11:25:48
How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of tags associated with it. models.py class post(models.Model): tag = models.ManyToManyField(Tag) text = models.CharField(max_length=100) serializers.py class PostSerializer(serializers.ModelSerializer): class Meta: model = Post fields = ("text", "tag"??) views.py class PostViewSet(viewsets.ReadOnlyModelViewSet): queryset = Post.objects.all() serializer_class = PostSerializer You will need a TagSerializer , whose class Meta