django-rest-framework

Nested Json is not being serialized while using Multipart Parser

南楼画角 提交于 2021-02-11 15:09:40
问题 I will try to give a small example and tell where it's not working. The models are as follows, class Address(models.Model): name = models.CharField(db_column='name', max_length=200, blank=False, null=False, unique=True) class Meta: managed = True db_table = 'Address' class Product(models.Model): title = models.CharField(db_column='title', max_length=200, blank=False, null=False) imageUrl = models.ImageField(db_column='image_url', blank=True, null=True, upload_to='deals/%Y/%m/') addresses =

Return custom response to successful POST request in django rest framework

眉间皱痕 提交于 2021-02-11 14:56:42
问题 I want to return a custom response to the user when they hit the API with a POST request and it's a success. Here are the code snippets : views.py class BlogPostAPIView(mixins.CreateModelMixin,generics.ListAPIView): # lookup_field = 'pk' serializer_class = BlogPostSerializer def get_queryset(self): return BlogPost.objects.all() def perform_create(self, serializer): serializer.save(user=self.request.user) def post(self,request,*args,**kwargs): return self.create(request,*args,**kwargs) urls.py

How do I append a list to FormData?

丶灬走出姿态 提交于 2021-02-11 14:52:33
问题 I want to send image with additional data from React.js to Django backend. When I used FormData() to create my form data, it could not send the array (because it converted it to string). Here is my code: let formData = new FormData(); formData.append('text', postForm.text); formData.append('image', postForm.image, postForm.image.name); formData.append('privacy', postForm.privacy); formData.append('custom_list', postForm.customList); props.createPost(formData); when I used this, I got this

Multi Nested with Django Restframework and Mongodb

落花浮王杯 提交于 2021-02-11 14:32:36
问题 I want output like this. I have a task to complete more than 5 models with nested type and all CRUD methods by using RestFawork.If anyone knows the answer share with me. { "id": 1, "first_name": "John 1", "last_name": "Robert 1", "instrument": "KeyBoard", "album_musician": [ { "id": 3, "artist": 1, "name": "Test robert", "release_date": "2020-09-16", "num_stars": 400 "Language": [ { "Original": "English", "Dubbed": "Tamil" } ] } ] } In this Code, I did only Sigle Nested Framework. My model.py

Add extra field with ManytoMany field in DRF

允我心安 提交于 2021-02-11 14:18:42
问题 I have am trying to create the model class on which i want to add the extra field with ManytoMany field. On some google search I have identified its possible to through on implementing I am getting couple of errors which i am not able to identify whats the issue. Here is my model class: class CompQuantity(models.Model): comp_code = models.ForeignKey( 'core.SensorDevice', on_delete=models.CASCADE, null=True ) quantity = models.IntegerField() class PackageComponent(models.Model): pkg_code =

Celery doesn't process the task request in single hit?

◇◆丶佛笑我妖孽 提交于 2021-02-11 14:17:58
问题 I have set up a Django project with Celery and Redis. I am trying to send an OTP to a mobile number. The problem is whenever I try to execute the task by running sms_queue_processor.delay(phone, message) the celery worker doesn't receive the task. I tried the same executing from the shell but it doesn't receive at all. I tried to execute the statement from the shell at a rapid speed twice then the celery worker receives and I can able to receive the SMS. This is something weird and can't be

How to use a different domain for Djoser email?

☆樱花仙子☆ 提交于 2021-02-11 13:47:40
问题 How can I change the domain the link inside the email djoser sends uses? 回答1: I figured this out, to change the link domain you need to add DOMAIN and SITE_NAME to your project settings. Example: DOMAIN = config('DOMAIN') #example.com SITE_NAME = config('SITE_NAME') #Example DJOSER = { 'LOGIN_FIELD':'email', 'USER_CREATE_PASSWORD_RETYPE':True, 'ACTIVATION_URL': '/users/activate/{uid}/{token}', 'SEND_ACTIVATION_EMAIL': True, 'SERIALIZERS':{ 'user_create':'userauth.serializers

Django-REST-Framework: ForeignKey instance is not passed to validated_data

泄露秘密 提交于 2021-02-11 13:31:38
问题 I'm trying to create an instance overriding create method and passing the ForeignKey instance in the data, but that field is not included in validated_data passed to create method. How can I pass that to create ?? I have this model. class UserProfile(models.Model): name = models.CharField(verbose_name='Name', max_length=50) email = models.EmailField(verbose_name='Email') address = models.TextField(verbose_name='Address', null=True, blank=True) user = models.OneToOneField(User, verbose_name=

Django-REST-Framework: ForeignKey instance is not passed to validated_data

天大地大妈咪最大 提交于 2021-02-11 13:28:58
问题 I'm trying to create an instance overriding create method and passing the ForeignKey instance in the data, but that field is not included in validated_data passed to create method. How can I pass that to create ?? I have this model. class UserProfile(models.Model): name = models.CharField(verbose_name='Name', max_length=50) email = models.EmailField(verbose_name='Email') address = models.TextField(verbose_name='Address', null=True, blank=True) user = models.OneToOneField(User, verbose_name=

PATCH method in django-rest returns 400 BAD REQUEST

為{幸葍}努か 提交于 2021-02-11 12:35:52
问题 I've been trying to setup my API to handle PATCH requests, but I keep getting the 400 BAD REQUEST response. I can't seem to figure out what I need to change in order for it to work. The get method is working the way it's supposed to, but the PATCH is evading me. serializers.py class HeroCounterSerializer(serializers.ModelSerializer): class Meta: model = HeroCounters fields = ('ct1', 'ct2', 'score', 'pk') views.py class HeroCounterViewSet(viewsets.ModelViewSet): queryset = HeroCounters.objects