#Django Queries

1 messages · Page 1 of 1 (latest)

thorny niche
#

Hi,

I need ideas on how to handle this logic. I have 2 models: Albums and Photos. The Photos model is related to the Albums model by a ForeignKey. Now, it could happen that I have multiple albums and multiples photos in each Album. How can I define this in the views such that only photos in each album is displayed rather than all photos in all albums? I am a bit confused on how to go about it. Thanks.

PS: This is in Django

cedar fog
glad citrus
#

@thorny niche I didn't understand what you need?

{
  album1: [photo1, photo2],
  album2: [photo3, photo4]
}
#

did you find it out

thorny niche
glad citrus
#

like Tim said, you pass the album id/slug parameter in url

#

do you have a separate frontend or you use django templates?

#
def get_album_photos(request, id):
  photos = Photo.objects.filter(album__id=id)
  ... get whatever you need
# urls

path("albums/<int:id>/photos/", get_album_photos)

something like this

cedar fog
thorny niche
glad citrus
#

the same logic applies

#

make an api call with album id/slug

#

then get photos for that album

#

which is what Tim said, why do you say not the solution?

thorny niche
thorny niche
thorny niche
glad citrus
#

class AlbumsView(APIView):
  def get(self, request):
    albums = Album.objects.all()
    serializer = YourAlbumSerializer
    return Response(serializer.data)


class AlbumsPhotosView(APIView):
  def get(self, request, album_id=None):
    photos = Photo.objects.filter(album__id=album_id)
    serializer = YourPhotosSerializer
    return Response(serializer.data)

class based view or function, doesnt matter

thorny niche
thorny niche
glad citrus
#

you are using DRF right?

#

using DjangoModelPermissions will use django's built in crud permissions for lookup

glad citrus
#

if most of your site requires authentication, it's always better to set base permission to IsAuthenticated in your settings

thorny niche
#

And thank you for the resource.

glad citrus
#

then you just use AllowAny permission on the views that doesn't require authentication

thorny niche
thorny niche
# glad citrus yes

Okay. which authentication framework or library would you suggestion? I tried Djoser but I do not understand how it works. Plus, I also need to use Djoser alongside a token authentication like JWT. How can I combine both?

thorny niche
glad citrus
#

if you want to implement social auth in future

#

i think it was django-allauth?

#

better look it up, can't say for sure

thorny niche
thorny niche
glad citrus
#

yes, you define request methods under the class based view @thorny niche

#

post/get/patch/put/delete..

#

look at GenericAPIView if you are working with models in a view (this is the case most of the time)

thorny niche
glad citrus
#

yes, otherwise you will get method not allowed error

thorny niche
glad citrus
#

yes, but it will ask you to set get_queryset method

thorny niche
glad citrus
#

or queryset attribute for the class

thorny niche
glad citrus
thorny niche
#

I really appreciate the time you have taken, but I have one more question as regards serializers. I read that they are a sort of replacement for forms in DRF. This means we will need to process the serializer when we want to use it in a view?

glad citrus
#

@thorny niche I would say yes and no. Serializer basically serializes your data json to python and vice-versa. But obviously it has many benefits like validation, custom validation, custom crud operations, representation of nested relations etc etc

#

but also comes with drawbacks

thorny niche
glad citrus
#

yes when you have 100 rows, 1000 rows of data, it seems all fine. when you have lots of data you start to notice the drawbacks (slow)

#

there are ways to improve performance, for example ModelSerializer is slow compared to base serializer serializers.Serializer because it has extra logic related to model

#

when you have a model

class User(Model):
  name
  full_name
  email
  age
  city
  ...

class UserSerializer(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = "__all__"

If I wanted to serialize User data, unless you have some advanced field calculations, I don't see the need to use serializer.

you could just do User.objects.values()

the speed difference is very noticable

#

but for create/update, I would use serializer because It's easier to implement custom validations and provides abstraction

#

very good article regarding this topic

#
In late 2013, Tom Christie, the creator of Django Rest Framework, wrote an article discussing some of DRF's drawbacks. In his benchmarks, serialization accounted for 12% of the total time spend on processing a single request. In the summary, Tom recommends to not always resort to serialization:

4. You don't always need to use serializers.

For performance critical views you might consider dropping the serializers entirely and simply use .values() in your database queries.
thorny niche
#

Oh, thank you so much for this. I really appreciate your help.

glad citrus
#

you are welcome