Categories
Python Answers

How to include related model fields using Django Rest Framework?

To include related model fields using Django Rest Framework, we can set the depth property in the serializer class.

For instance, we write

class ClassroomSerializer(serializers.ModelSerializer):
    class Meta:
        model = Classroom
        depth = 1

to set the depth field in the Meta class in the ClassroomSerializer to 1 to include the model fields directly related to Classroom.

Categories
Python Answers

How to set a dynamic File Path in Python Django?

To set a dynamic File Path in Python Django, we can create a function that returns the file path we want.

For instance, we write

import os

def get_upload_path(instance, filename):
    return os.path.join(
      "user_%d" % instance.owner.id, "car_%s" % instance.slug, filename)

to create the get_upload_path function to return the file path we want given the filename and the model instance.

Then we set that as the upload_to argument of the model field by writing

photo = models.ImageField(upload_to=get_upload_path)

to create the photo ImageField with upload_to set to get_upload_path.

Categories
Python Answers

How to override a parent model’s attribute with Python Django?

To override a parent model’s attribute with Python Django,. we can create a child model that inherits from the parent.

For instance, we write

class AbstractPlace(models.Model):
    name = models.CharField(max_length=20)
    rating = models.DecimalField()

    class Meta:
        abstract = True

class Place(AbstractPlace):
    pass

class LongNamedRestaurant(AbstractPlace):
    name = models.CharField(max_length=255)
    food_type = models.CharField(max_length=25)

to create the AbstractPlace model which has some fields shared between Place and LongNamedRestaurant.

Then in LongNamedRestaurant, we override the name field from AbstractPlace by setting name to a models.CharField(max_length=255) instead of models.CharField(max_length=20).

Categories
Python Answers

How to do reverse lookup of foreign keys with Python Django?

To do reverse lookup of foreign keys with Python Django, we can use the filter method.

For instance, we write

def detail(request, venue_id):
    venue = Event.objects.filter(venue__id=venue_id)
    return render(request, 'venue-detail.html', {'venue': venue})

to look up the Event objects by the venue_id with filter.

And then we render the values in the venue-detail.html.

Categories
Python Answers

How to show Image from Imagefield with Python Django Admin?

To show Image from Imagefield with Python Django Admin, we can add our own tag.

For instance, w write

def image_tag(self):
    from django.utils.html import escape
    return u'<img src="%s" />' % escape(<URL to the image>)
image_tag.short_description = 'Image'
image_tag.allow_tags = True

to add the image_tag that returns a string with the img element.

Then in admin.py, we reguister the tag with

fields = ( 'image_tag', )
readonly_fields = ('image_tag',)