question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEEDBACK] Clarify with DRF

See original GitHub issue

I am curious to know so wants to clarify

in https://phalt.github.io/django-api-domains/plugins/#django-rest-framework

you wrote

When using DRF, we can organise the logic in a domain this way:

urls.py - Router and URL configuration. apis.py - DRF view functions or view classes. serializers.py - Serialization for models.

and

https://phalt.github.io/django-api-domains/files (let’s call this Files section)

you have a

  • models.py
  • apis.py
  • interfaces.py
  • services.py

Plus you have this helpful diagram https://phalt.github.io/django-api-domains/styleguide/#visualisation

Can you help me to understand how the files work with DRF situation? Perhaps another diagram?

Because I cannot visualize how to use DRF on top of what you recommend under Files

Another related question is

DO you recommend the use of GenericAPIView or APIView classes in DRF for the apis.py?

Do you mind giving a visual idea of how two different domains both using DRF would interact with one another similar to https://phalt.github.io/django-api-domains/examples/ ?

What if they are different apps of the same Django project?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
voyc-jeancommented, May 7, 2020

In my experience with experimenting with this styleguide, how you structure your views in apis.py doesn’t matter all that much, as long as it includes presentation logic. Personally, I make use of APIView and only use the put/patch/get/update/delete methods. You could simply have something like this:

# apis.py
class PublishPostView(APIView):
    def post(self, request, *args, **kwargs):
        result = PostService.publish(post_uuid=kwargs['pk'])
        return Response(result)
# urls.py
from .apis import PublishPostView
urlpatterns = [
    path(
        'posts/<uuid:pk>/publish',
        PublishPostView.as_view(),
        name='publish_post',
    ),
]

I also make use of serializers in apis.py to validate inbound data and to return serializable data.

1reaction
phaltcommented, Apr 30, 2020

So many questions!

Let me tick off a few of the easy ones:

  • I almost always use APIView. This is a personal preference, because I like the get/post/put/delete methods on the class, I find the retrieve/update/??/?? paradigm on viewsets hard to remember (I don’t actually remember them) and it is jarring for new users. APIView is really simple and works.

  • If your django app is providing an API with DRF, then you just use the existing file format as I recommended, but place particular DRF logic in the files I recommened above (so add a urls.py to your domain to handle URLs, for example).

  • If you have two Domains in a single Django project, then why are you using DRF to talk between the two? Just use internal software APIs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django-rest-framework: How do I create a Feedback / Contact ...
I'm trying to create a simple contact option using Django Rest Framework. The contact page would allow users and non-users to send the...
Read more >
Django REST Framework Views - APIViews - TestDriven.io
This article takes a deep dive into how Django REST Framework's views work and its most basic view, APIView.
Read more >
Validate the user if the review for the book already exists or ...
I'm using the Django rest framework to implement a books reviews app where I ... the user for 2nd review for that book...
Read more >
Login and Register User — Django Rest Framework - Medium
Login and Register User — Django Rest Framework. Build a Product Review Backend with DRF — Part 8 ... Let's explain this code....
Read more >
What is your opinion on the Django REST framework? - Quora
Let me explain. Ease : PHP has been out there for more than 20 years. It's been used and overused by millions of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found