'Create model with a link to static-file when created

I have a model

class myModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE,null=True)
    start_price = models.FloatField(default=0)
    image_url = models.URLFild(max_length=512,default="/static/media/wait_logo.png' %}") #Issue

which links to an initial image (wait_logo.png) when created.

The problem is that it's not very automatic, if I change my static-folder. In the template; if I want to refer to a static image I can use

<a href="{% static 'media/wait_logo.png' %}

is there a way to extract the same static-path in models.py and use that in the default-value?



Solution 1:[1]

I figured out, that we can get the static_url by

from django.conf import settings

static_url = settings.STATIC_URL

class myModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE,null=True)
    start_price = models.FloatField(default=0)
    image_url = models.URLFild(max_length=512,default=static_url+"media/wait_logo.png' %}") #Fixed


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CutePoison