Questions tagged [django-rest-framework]

A powerful and flexible toolkit for building RESTful Web APIs. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

A Web API framework for Django. Suitable for building complex, highly customizable APIs and web services.

Features include:

  • Built-in support for various authentication, permissions, versioning, throttling and other API policies.
  • Creates APIs that can be interacted with directly in the browser (also known as Browsable APIs).
  • Large ecosystem of third party packages.

Documentation | GitHub | Mailing list


Related tags:

23552 questions
117
votes
10 answers

Django Rest Framework: Dynamically return subset of fields

Problem As recommended in the blogpost Best Practices for Designing a Pragmatic RESTful API, I would like to add a fields query parameter to a Django Rest Framework based API which enables the user to select only a subset of fields per…
Danilo Bargen
  • 15,862
  • 15
  • 79
  • 111
115
votes
1 answer

When to use Serializer's create() and ModelViewset's create() perform_create()

I want to clarify the given documentation of django-rest-framework regarding the creation of a model object. So far I found that there are 3 approaches on how to handle such events. The Serializer's create() method. Here is the documentation class…
Roel
  • 6,512
  • 10
  • 59
  • 100
111
votes
17 answers

Django Rest Framework File Upload

I am using Django Rest Framework and AngularJs to upload a file. My view file looks like this: class ProductList(APIView): authentication_classes = (authentication.TokenAuthentication,) def get(self,request): if…
Pawan
  • 3,521
  • 6
  • 25
  • 32
108
votes
2 answers

ModelSerializer using model property

I'm trying to serialize a model containing a property field that I also want to serialize. models.py: class MyModel(models.Model): name = models.CharField(max_length=100) slug = models.AutoSlugField(populate_from='name') @property …
Sander Smits
  • 1,701
  • 2
  • 15
  • 16
106
votes
5 answers

How to change field name in Django REST Framework

I am trying to change Model field name in DRF Serializer like alias in SQL. I have tried different methods but cannot succeed. models.py class Park(models.Model): name = models.CharField(max_length=256) alternate_name =…
Shoaib Ijaz
  • 4,373
  • 8
  • 43
  • 77
104
votes
28 answers

django.db.migrations.exceptions.InconsistentMigrationHistory

When I run python manage.py migrate on my Django project, I get the following error: Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/home/hari/project/env/local/lib/python2.7/site-…
103
votes
7 answers

Retrieving a Foreign Key value with django-rest-framework serializers

I'm using the django rest framework to create an API. I have the following models: class Category(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return self.name class Item(models.Model): name =…
hellsgate
  • 5,253
  • 5
  • 30
  • 45
102
votes
4 answers

What's the appropriate HTTP status code to return if a user tries logging in with an incorrect username / password, but correct format?

A similar question is posted here: What's an appropriate HTTP status code to return by a REST API service for a validation failure? The answer in the thread above states that "For instance if the URI is supposed to have an ISO-8601 date and you find…
99
votes
7 answers

Python Django Rest Framework UnorderedObjectListWarning

I upgraded from Django 1.10.4 to 1.11.1 and all of a sudden I'm getting a ton of these messages when I run my tests: lib/python3.5/site-packages/rest_framework/pagination.py:208: UnorderedObjectListWarning: Pagination may yield inconsistent results…
Denise Mauldin
  • 4,205
  • 2
  • 24
  • 42
97
votes
10 answers

Django: TemplateDoesNotExist (rest_framework/api.html)

In my view function, I'd like to return a json object (data1) and some text/html (form). Is this possible? MY code @api_view(['POST']) @permission_classes((AllowAny,)) def create_user(request): if request.is_ajax(): if request.method ==…
Coeus
  • 1,745
  • 2
  • 13
  • 25
96
votes
7 answers

Django rest framework serializing many to many field

How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of tags associated with it. models.py class post(models.Model): tag =…
95
votes
11 answers

Django rest framework nested self-referential objects

I have model that looks like this: class Category(models.Model): parentCategory = models.ForeignKey('self', blank=True, null=True, related_name='subcategories') name = models.CharField(max_length=200) description =…
Jacek Chmielewski
  • 1,474
  • 1
  • 14
  • 16
94
votes
8 answers

Django Rest Framework with ChoiceField

I have a few fields in my user model that are choice fields and am trying to figure out how to best implement that into Django Rest Framework. Below is some simplified code to show what I'm doing. # models.py class User(AbstractUser): …
awwester
  • 7,642
  • 9
  • 37
  • 63
92
votes
22 answers

Django Rest Framework -- no module named rest_framework

I've installed django rest framework using pip install djangorestframework yet I still get this error when I run "python3 manage.py sycndb": ImportError: No module named 'rest_framework' I'm using python3, is this my issue?
tryingtolearn
  • 2,066
  • 5
  • 18
  • 40
92
votes
5 answers

How do you filter a nested serializer in Django Rest Framework?

In Django Rest Framework, how do you filter a serializer when it's nested in another serializer? My filters are imposed in the DRF viewsets, but when you call a serializer from inside another serializer, the viewset of the nested serializer never…
John
  • 3,430
  • 4
  • 19
  • 34