django-rest-framework

'Can\'t connect to MySQL server on \'db\' Django-Restframework with Mysql in docker

拟墨画扇 提交于 2020-06-28 05:49:10
问题 I have dockerized my existing Django Rest project which uses MySQL database. Dockefile FROM python:3.6 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code COPY . /code/ RUN pip install -r requirements.txt requirements.txt django djongo django-rest-framework wheel pillow mysqlclient django-cors-headers docker-compose.yml version: '3' volumes: portainer: services: db: image: mysql environment: MYSQL_ROOT_PASSWORD: docker MYSQL_DATABASE: docker MYSQL_USER: docker MYSQL_PASSWORD: docker ports: -

How do I authenticate users with Django REST framework and React.js frontend?

别说谁变了你拦得住时间么 提交于 2020-06-27 15:09:11
问题 I am making a website with django rest in the backend and react.js in the frontend. This will be my first time using a front end framework with django. I am using django-allauth for social auth on the backend (facebook/twitter/google). Once the user is signed in through a web session (using allauth) via a 3rd party provider (facebook) how do I pass that data to my react app? Do I grab the facebook token, authenticate it, then generate an oauth2/jwt token and store that somehow? Is there some

How do I authenticate users with Django REST framework and React.js frontend?

孤街浪徒 提交于 2020-06-27 15:07:28
问题 I am making a website with django rest in the backend and react.js in the frontend. This will be my first time using a front end framework with django. I am using django-allauth for social auth on the backend (facebook/twitter/google). Once the user is signed in through a web session (using allauth) via a 3rd party provider (facebook) how do I pass that data to my react app? Do I grab the facebook token, authenticate it, then generate an oauth2/jwt token and store that somehow? Is there some

Django Rest Framework regroup queryset by a category

余生长醉 提交于 2020-06-27 12:23:28
问题 In the current project that i'm working on, i need to regroup (group) a queryset by category and put contents with same category in a list all provided together. I have the following model structure: class Category(models.Model): title = models.CharField(max_length=255) class Item(models.Model): title = models.CharField(max_length=255) category = models.ForeignKey(to="Category", verbose_name=_('category'), related_name='items', on_delete=models.SET_NULL, null=True, blank=True) I would like

Override serializer delete method in Django RF

强颜欢笑 提交于 2020-06-27 07:36:39
问题 I have a serializer that inherits from the django rest framework serializer ModelSerializer . To overwrite the create method, I can redefine create . To redefine the update method, I redefine update . I'm looking through the code though and can't find the method to overwrite for deletion. I need to do this in the serializer so I can grab the deleting user. Any thoughts would be appreciated! 回答1: If you're using a ModelViewSet, you could do it in the view: class YourViewSetClass(ModelViewSet):

Override serializer delete method in Django RF

妖精的绣舞 提交于 2020-06-27 07:34:10
问题 I have a serializer that inherits from the django rest framework serializer ModelSerializer . To overwrite the create method, I can redefine create . To redefine the update method, I redefine update . I'm looking through the code though and can't find the method to overwrite for deletion. I need to do this in the serializer so I can grab the deleting user. Any thoughts would be appreciated! 回答1: If you're using a ModelViewSet, you could do it in the view: class YourViewSetClass(ModelViewSet):

DRF what's the difference between serializers(many=True) vs ListField(serializer())

自作多情 提交于 2020-06-27 03:37:09
问题 When should I use each of the following? * MySerializer(many=True) * serializers.ListField(child=MySerializer()) 回答1: MySerializer(many=True) - you can use when MySerializer makes some serialization of each of your objects. serializers.ListField(child=MySerializer()) - this one you can use if you want to serialize not only objects, but result iterable data too. For example, you want to append for each object its position in result list: class ListSerializerWithIds(serializers.ListSerializer):

Value error on Image field when trying to comvert model to dict

丶灬走出姿态 提交于 2020-06-26 14:48:06
问题 I have 2 models, 1st is Garage class Garage(models.Model): name = models.CharField(verbose_name=_('Garage'), max_length=200) @property def cars(self) -> list: from django.forms.models import model_to_dict cars = [] for i in Car.objects.filter(garage=self.id): cars.append(model_to_dict(i)) return cars def __str__(self): return self.name class Meta: verbose_name = _('Garage') My 2nd model is Car, class Car(models.Model): name = models.CharField(verbose_name=_('Car Name'), max_length=200) garage

How to sort values of serializer custom field in DRF

泪湿孤枕 提交于 2020-06-26 07:43:45
问题 I have created a custom field in the client serializer. The value of this field is calculated by a complex serializer method. class ClientsStatsSerializer(serializers.ModelSerializer): """ Serializer shows total_spend for 2019 by client. """ refs_count = serializers.SerializerMethodField() total_spend_2019 = serializers.SerializerMethodField() class Meta: model = Company ordering = ('total_spend_2019',) fields = [ 'id', 'legal_name', 'refs_count', 'total_spend_2019', ] def get_total_spend

How to build a json REST API in django (without Django REST framework)

时光毁灭记忆、已成空白 提交于 2020-06-25 17:08:51
问题 Preface I have a django project. I've wired it up so it serves a bunch of views for a bunch of models. Now I want to add an endpoint which just dumps a good fraction of the database out as json. The way I would assume you do this is add a URL to a view class / method which returns a HTTPResponseObject full of json. Unfortunately, after quite a bit of googling, all I can find are references to Django REST framework. This is the sort of thing you would think Django would provide internally not