django-rest-framework

How to properly serve my Angular application static files from a Django project on Heroku?

99封情书 提交于 2021-02-19 03:08:47
问题 I am trying to deploy on Heroku my app built with Django 2, DRF and Angular 6, and struggling to get the static files of my Angular app served. I am using WhitheNoise for both development environment and on Heroku. The structure of the project is as follows: my_project/ |-frontend/ | |- dist/ | |- ... |-myproject/ | |-settings/ | | |- __init.py | | |- base.py | | |- prod.py | | |- dev.py | |- __init__.py | |- urls.py | |- views.py | |- wsgi.py |-other_app1/ | |- ... |-other_app2/ | |- ... |

Pass context from one serializer to another?

谁说我不能喝 提交于 2021-02-19 02:35:09
问题 With Django REST Framework, I have 2 serializers: PageSerializer and CommentSerializer . CommentSerializer depends on some extra "context" value, but it doesn't get it directly, instead it needs to get it from PageSerializer , since they have a nested relationship. So I need to have something like this: class CommentSerializer(serializers.ModelSerializer): ... my_field = serializers.SerializerMethodField() def get_my_field(self, comment): my_value = self.context['my_value'] ... class

Token in query string with Django REST Framework's TokenAuthentication

醉酒当歌 提交于 2021-02-18 22:55:41
问题 In an API built with Django REST Framework authentication can be done using the TokenAuthentication method. Its documentation says the authentication token should be sent via an Authorization header. Often one can send API-keys or tokens via a query string in order to authenticate, like https://domain.com/v1/resource?api-key=lala . Is there a way to do the same with Django REST Framework's TokenAuthentication? 回答1: By deafult DRF doesn't support query string to authenticate, but you can

Token in query string with Django REST Framework's TokenAuthentication

Deadly 提交于 2021-02-18 22:55:07
问题 In an API built with Django REST Framework authentication can be done using the TokenAuthentication method. Its documentation says the authentication token should be sent via an Authorization header. Often one can send API-keys or tokens via a query string in order to authenticate, like https://domain.com/v1/resource?api-key=lala . Is there a way to do the same with Django REST Framework's TokenAuthentication? 回答1: By deafult DRF doesn't support query string to authenticate, but you can

Where can I access request parameters in Django Rest Framework?

大兔子大兔子 提交于 2021-02-18 22:37:07
问题 I'm using Django Rest Framework and python-requests and passing several variables through the URL as shown below. "GET /api/boxobjects/?format=json&make=Prusa&model=i3&plastic=PLA HTTP/1.1" So I'm passing the variables make, model, and plastic. The recommended method to access these parameters is shown below. makedata = request.GET.get('make', '') However, I have no idea where to place that line of code. I've completed the tutorial for Django Rest Framework and have my views set up to roughly

Where can I access request parameters in Django Rest Framework?

*爱你&永不变心* 提交于 2021-02-18 22:36:46
问题 I'm using Django Rest Framework and python-requests and passing several variables through the URL as shown below. "GET /api/boxobjects/?format=json&make=Prusa&model=i3&plastic=PLA HTTP/1.1" So I'm passing the variables make, model, and plastic. The recommended method to access these parameters is shown below. makedata = request.GET.get('make', '') However, I have no idea where to place that line of code. I've completed the tutorial for Django Rest Framework and have my views set up to roughly

Posting a foreign key relationship in Django Rest Framework

霸气de小男生 提交于 2021-02-18 05:23:39
问题 In my models , I have the following classes: class Topic(models.Model): name = models.CharField(max_length=25, unique=True) class Content(models.Model): title = models.CharField(max_length=255) body = models.TextField() topic = models.ForeignKey(Topic, blank=True, null=True) My serializers is like this: class TopicSerializer(serializers.ModelSerializer): class Meta: model = Topic fields = ('name') class ContentSerializer(serializers.ModelSerializer): topic = TopicSerializer(read_only=True)

Django - How to filter by date with Django Rest Framework?

依然范特西╮ 提交于 2021-02-17 21:35:29
问题 I have some model with a timestamp field: models.py class Event(models.Model): event_type = models.CharField( max_length=100, choices=EVENT_TYPE_CHOICES, verbose_name=_("Event Type") ) event_model = models.CharField( max_length=100, choices=EVENT_MODEL_CHOICES, verbose_name=_("Event Model") ) timestamp = models.DateTimeField(auto_now=True, verbose_name=_("Timestamp")) I'm then using Django-rest-framework to create an API endpoint for this class, with django-filter providing a filtering

Django REST Framework: raise error when extra fields are present on POST

寵の児 提交于 2021-02-17 21:13:32
问题 When you're writing a serializer, it is trivial to specify which fields will be included (via Meta 's fields ), setting read/write permissions on them and validating them. However, I was wondering if there is an easy way to specify that only the fields that are included are to be expected and any extra keys passed in should raise an error. E.g. say, I have a serializer class ModelASerializer(serializers.ModelSerializer): class Meta: model = models.ModelA fields = ('name', 'number') Supposed

Django REST Framework: raise error when extra fields are present on POST

流过昼夜 提交于 2021-02-17 21:12:02
问题 When you're writing a serializer, it is trivial to specify which fields will be included (via Meta 's fields ), setting read/write permissions on them and validating them. However, I was wondering if there is an easy way to specify that only the fields that are included are to be expected and any extra keys passed in should raise an error. E.g. say, I have a serializer class ModelASerializer(serializers.ModelSerializer): class Meta: model = models.ModelA fields = ('name', 'number') Supposed