These are chat archives for django/django

3rd
Dec 2017
MilanRgm
@MilanRgm
Dec 03 2017 14:01
why am i getting 'RelatedManager' object does not support indexing error? As I want to show first image as banner image and other image as gallery like
class Furniture(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True)
    slug = models.SlugField(max_length=200, unique=True)

    def first_image(self):
        """
        Return first image of the furniture
        """
        return self.furniture_pics[0]

class FurniturePic(models.Model):
    """
    Represents furniture picture
    """
    furniture = models.ForeignKey(Furniture, related_name='furniture_pics')
    url = models.ImageField(upload_to=upload_image_path)
Deniz Saner
@denizs
Dec 03 2017 14:05
You need to return self.furniture_pics.first()
By setting a related_name, you define a manager, just like Model.objects. Hence, you will need to call a method on it
MilanRgm
@MilanRgm
Dec 03 2017 14:23
oh wow thanks for the explanation
I get <img src="4nitures/3766431209/3766431209.jpg" alt="" class="img-responsive"> following path in src but the image is not shown and when i open the image in new tab i get
Furniture matching query does not exist.
I did the following <img src="{{ furniture.first_image.url }}" alt="" class="img-responsive">
MilanRgm
@MilanRgm
Dec 03 2017 14:29
the route should be something like this http://localhost:8000/media/4nitures/3766431209/3766431209.jpg but it is http://localhost:8000/furniture/4nitures/3766431209/3766431209.jpg
MilanRgm
@MilanRgm
Dec 03 2017 14:34
I had to do /media/{{ furniture.first_image.url }} . Is this the expected one?
Costantin
@Cosbgn
Dec 03 2017 15:35
hey, is anyone using docker?
it seems that I always need to run docker-compose up
and nginx doens't have the hot-reload feature the django manage.py runserver has
any solution for that?
Deniz Saner
@denizs
Dec 03 2017 16:29
@MilanRgm The exception is raised because one of your model instances doesn’t habt any picture instance related to it. You should wrap your query in a try/except and handle the edge case accordingly
Peter Bittner
@bittner
Dec 03 2017 19:32
@MilanRgm For the image URLs you should probably use staticfiles. https://docs.djangoproject.com/en/stable/howto/static-files/
Deniz Saner
@denizs
Dec 03 2017 22:53
In case your deployment environment is AWS you could also have a look at the storages app
Which kind of extends django’s static file handling