django-serializer

Customize Json Response in django rest framework

走远了吗. 提交于 2019-12-06 11:07:20
Given below is my serializer class. I have all fields in one model.I would like to change the representation of serializer data in custom format. Tried to_representation method of serializer but could not success. class MyListSerilizer(ModelSerializer): class Meta: model=MyModel fields=['Name','Address_ID','Portal','Address','City','DisplayOrderDateTime','Email','Order_ID','Order_Code','Item_Code','DispatchedOn','Payment_Mode','Shipping_Charge','ShippingMethodCode','ShippingMethodCharges','CashOnDeliveryCharges','CurrencyCode','BillingAddress','GiftWrapCharges','SaleOrderItemCode','Shipping

django-rest-framework serializer different fields in multiple views

我与影子孤独终老i 提交于 2019-12-05 23:26:25
问题 I am new at Django and couldn't find solution for my problem. The problem is to force specific serializer for include different amount of fields in case of utilizing different views. I would like to use 'id' field in my 1st view, and in 2nd view - 'id' and 'name' fields. Here is my model.py class Processing(models.Model): id = models.AutoField(primary_key=True) name = models.CharField() description = models.CharField() And here is my serializer.py class ProcessingSerializer(serializers

DjangoRestFramework ModelSerializer: field-level validation is not working

天涯浪子 提交于 2019-12-05 09:42:28
This is my serializers.py (I want to create a serializer for the built-in User model): from rest_framework import serializers from django.contrib.auth.models import User class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('username', 'password', 'email', ) def validate_username(self, username): if not re.search(r'^\w+$', username): #checks if all the characters in username are in the regex. If they aren't, it returns None raise serializers.ValidationError('Username can only contain alphanumeric characters and the underscore.') try: User.objects.get(username

serialize model object with related objects to JSON

故事扮演 提交于 2019-12-05 07:11:17
I am having a Person model to store person details. class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthdate = models.DateField() also i am having model PersonLogs to store person's activity logs. class PersonLogs(models.Model): person = models.ForeignKey(Person) time = models.DateTimeField(auto_now_add=True) I am using Django Serializer to return Person objects into JSON format as response. from django.core import serializers data = serializers.serialize("json", Person.objects.all()) Output : { "model": "store.person",

Pass json and deserialize form in django

随声附和 提交于 2019-12-04 10:38:31
The issue is next: I send some post data with ajax to server. This data looks like: data = { form : $(this).serialize(), some_array: [2,3,4,1] } How to get form object in django? request.POST['form'] returns a string with a form. I'm trying to use import json library. But, when I run value = json.load(request.POST['some_array']) or form = json.load(request.POST['form']) it doesn't work. Printing request.POST['form'] returns the following: u'csrfmiddlewaretoken=I3LWAjfhZRGyd5NS3m9XcJkfklxNhxOR& address_city=%D0%9A%D0%B8%D1%97%D0%B2& address_street=2& address_building=3& delivery_time=2015-05-15

django-rest-framework serializer different fields in multiple views

余生长醉 提交于 2019-12-04 04:48:53
I am new at Django and couldn't find solution for my problem. The problem is to force specific serializer for include different amount of fields in case of utilizing different views. I would like to use 'id' field in my 1st view, and in 2nd view - 'id' and 'name' fields. Here is my model.py class Processing(models.Model): id = models.AutoField(primary_key=True) name = models.CharField() description = models.CharField() And here is my serializer.py class ProcessingSerializer(serializers.ModelSerializer): id = serializers.ModelField(model_field=Processing()._meta.get_field('id')) class Meta:

Access auto increment primary field value during creation of a record with django rest framwork

断了今生、忘了曾经 提交于 2019-12-02 19:16:58
问题 I am trying to save 3 tables at once and the data is getting saved in the table But I am having issues to get inserted visit_id, vdata_id, vchild_id from each of these table. (it is the primary key with auto_created = True settings) to return the value in the api. I have tried many ways to achieve this. one attempt was this VisVisits.objects.latest('visit_id') but it gives an error something similar to <visit_id 46> is not serializer sharing my code: serializer class VisVisitsSerializer

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

邮差的信 提交于 2019-12-02 16:44:43
I want to create a many-to-many relationship where one person can be in many clubs and one club can have many persons. I added the models.py and serializers.py for the following logic but when I try to serialize it in the command prompt, I get the following error - What am I doing wrong here? I don't even have a HyperlinkedIdentityField Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Users\user\corr\lib\site-packages\rest_framework\serializers.py", line 503, in data ret = super(Serializer, self).data File "C:\Users\user\corr\lib\site-packages\rest_framework

Access auto increment primary field value during creation of a record with django rest framwork

天涯浪子 提交于 2019-12-02 10:17:22
I am trying to save 3 tables at once and the data is getting saved in the table But I am having issues to get inserted visit_id, vdata_id, vchild_id from each of these table. (it is the primary key with auto_created = True settings) to return the value in the api. I have tried many ways to achieve this. one attempt was this VisVisits.objects.latest('visit_id') but it gives an error something similar to <visit_id 46> is not serializer sharing my code: serializer class VisVisitsSerializer(serializers.ModelSerializer): data = VisVisitDataSerializer(many=True) data_child = VisVisitChildSerializer

Django Rest Framework: Writable nested serializers with Generic Foreign Key

北城以北 提交于 2019-12-01 04:13:00
问题 There are examples how to create a writable nested serializer like this and then how to serialize a generic foreign key (here). But I cannot find how to do both at the same time, i.e how to create a nested writable serializer for a generic foreign key field. In my models there is a Meeting model with a GenericForeignKey which can be either DailyMeeting or WeeklyMeeting like: class Meeting(models.Model): # More fields above content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE