django-serializer

Django: nest the object I'm serializing into the serializer?

依然范特西╮ 提交于 2020-01-07 06:47:33
问题 I'm looking to nest the object I'm serializing. Here's what I mean: My current UserSerializer: class UserSerializer(serializers.ModelSerializer): posts = serializers.SerializerMethodField() class Meta: model = User fields = ('__all__') def get_posts(self, user): posts = Posts.objects.get_posts_for_user(user=user) return PostsSerializer(posts, many=True, context=self.context) Here's my PostsSerializer: class PostsSerializer(serializers.ModelSerializer): class Meta: model = Posts fields = ('_

how to define a userprofile into UserDetialsSerializer?

女生的网名这么多〃 提交于 2020-01-07 01:48:57
问题 I want to be able to access a userprofile instance through : profile = instance.userprofile statement in UserSerializer instance is created through: instance = super(UserSerializer, self).update(instance, validated_data) statement in UserSerializer Since UserSerializer is inheriting UserDetailsSerializer , i think i should define a userprofile in UserDetailsSerializer . But i dont know how to do it ? Question: How to define userprofile in UserDetailsSerializer to achieve the above ?

Django get class from string

心已入冬 提交于 2020-01-06 11:34:13
问题 I'm looking for a generic way in Python to instantiate class by its name in similar way how it is done in Java without having to explicitly specify the class name in IF..ELIF condition. This is because I have several different models and serializers and want to make them addressable by parameters in the HTTP request. It is to enhance loose coupling and modularity. For example https://www.domain.com/myapp/sampledata.json?model=<modelname> should get the classes <modelname> and <modelname

how to override returned serializer object that is returned with the response django rest framework serializer

。_饼干妹妹 提交于 2020-01-06 06:44:12
问题 I have a django rest framework project. I want to override the returned json object structure when a get request is made to display a specific way not similar to the database structure. My current return object is displayed like so: { "id": 9, "namespace": "steve", "path": "something/another", "value": "this is a value", "person": 1 }, { "id": 11, "namespace": "namespace1", "path": "path2", "value": "anyoher value", "person": 2 }, { "id": 12, "namespace": "namespace1", "path": "path3", "value

DRF - ModelSerializer with a non-model write_only field

流过昼夜 提交于 2020-01-06 02:50:08
问题 I have the following Model, Serializer and View. My aim is to pass a custom string like referrer = "pid=email&af_sub1=ui_1120&c=xyz" in the POST method (RegisterViewSet below) and then to the viewset/serializer to use this information to fill in the referral_campaign, referral_media and inviting_user So: 1. referrer is write_only field 2. referrer is not a field in the model, but the info would be used to populate fields in the model How to achieve this in the DRF way? Model class User

Unique validation on nested serializer on Django Rest Framework

丶灬走出姿态 提交于 2019-12-28 04:13:45
问题 I have a case like this, where you have a custom nested serializer relation with a unique field. Sample case: class GenreSerializer(serializers.ModelSerializer): class Meta: fields = ('name',) #This field is unique model = Genre class BookSerializer(serializers.ModelSerializer): genre = GenreSerializer() class Meta: model = Book fields = ('name', 'genre') def create(self, validated_data): genre = validated_data.pop('genre') genre = Genre.objects.get(**genre) return Book.objects.create(genre

How to select specify field only in Viewset Django

你说的曾经没有我的故事 提交于 2019-12-24 10:49:33
问题 I would like to select the distinct field and some field only from Django ViewSet. My method @action(methods=['get'], detail=False) def shiftsum(self, request): query = ( Shift.objects.values('shiftid', 'dayname') .annotate(shiftdesc=Max('shiftdesc')) .annotate(ct=Count('*')) # get count of rows in group .order_by('shiftid', 'dayname') .distinct() ) serializer = self.get_serializer_class()(query,many=True) return Response(serializer.data) My Model class Shift(models.Model): shiftid = models

serialize django model queryset with serializers.serialize() function

淺唱寂寞╮ 提交于 2019-12-24 08:59:36
问题 How can I return JSON response of model queryset from a view using django serializer ? from django.core import serializers from django.http.response import JsonResponse def some_view(request): qs = SomeModel.objects.all() serialized_obj = serializers.serialize('json', qs) return JsonResponse(serialized_obj, safe=False) According to code snippet, the view producess a non-json response. 回答1: This can be easily done by using python format. serialized_obj = serializers.serialize( 'python' , qs)

Deserialize POST request with subset of serializer fields in DRF

浪尽此生 提交于 2019-12-24 04:06:14
问题 I'm having a rather simple problem, but found a few solutions and couldn't stop wondering what the intended DRF approach would be. I have a (simplified) model and serializer like this: class CartProduct(models.Model): cart = models.ForeignKey('Cart', on_delete=models.CASCADE) product = models.ForeignKey('Product', on_delete=models.CASCADE) class CartProductSerializer(serializers.HyperlinkedModelSerializer): id = serializers.ReadOnlyField() product = ProductSerializer() class Meta: model =

How to get all the properties of objects using foreign key in django model

别说谁变了你拦得住时间么 提交于 2019-12-23 05:42:09
问题 Using Djangorestframework I had created rest api. I have two models in my app countries and states. I had related countries model to states model using Foreign key method, But while fetching list of states in States api i am getting states names, but in the place of country i am getting countries primary key id instead of it's name how can i get all the fields of Countries instead of PK id ---------- Models.py code class countries(models.Model): country = models.CharField(max_length=10) def _