22

i know you will say that this question is asked before many times but i havent solved it yet...

models.py

class Doc(UploadModel):
    doc_no =  models.CharField(max_length=100, verbose_name = "No", blank=True)
    date_added = models.DateTimeField(verbose_name="Date", default=datetime.now,
                 editable=False)

class DocImage(models.Model):
    property = models.ForeignKey(Doc, related_name='images')
    image = FileBrowseField("Docs", max_length=200,
            directory="doc_img/%Y/%m/%d/%H/%M/%S/", 
            extensions=[".jpg",".tif"], blank=True, null=True)

views.py

def doc_detail(request, dosc_no):

    res = Doc.objects.filter(doc_no = dosc_no)        
    return render_to_response("doc/doc_detail.html",  {"result": res})

templates:

{% for i in docimage.property_set.all %}

{{ i.image.url }}  

{% endfor %}

i have tried above template but i didnt get any result. so i want to get imageurl adress in DocImage class...

all helps

urcm
  • 2,274
  • 1
  • 19
  • 45
  • Maybe its not included above, but where in your view are you passing `docimage` to the template context? All I can see is passing `result`. Also, to access the document from the DocImage class, you should just be able to use `property`. The `***_set` notation is used on the parent class where you don't have an explicit related_name set – will-hart Sep 05 '12 at 12:59
  • @ will-hart can you share a code for your answer to get result? – urcm Sep 05 '12 at 13:14

2 Answers2

47

If you review the foreign key documentation, if you have a relationship like

Doc -> has many DocImages

you need to define your foreign key on the DocImages class like so:

class DocImage(models.Model):
    property = models.ForeignKey(Doc, related_name='images')

If you don't set related names, you can access the DocImages from the Doc like:

Doc.docimage_set.all()

Docs on Related Objects

But setting related_name in the property field lets you do

Doc.images.all()

Just make sure whatever you pass to the template in the view context matches what is used in the template, e.g.

# in the view
return render_to_response('mytemplate.html', { 'mydoc' : doc, 'mydocimage' : img }

This can then be used in the template as follows:

# and in your template to get the images attached to the document
{% for i in mydoc.images.all %}
    ...
{% endfor %}

# or to get the document the image belongs to
{{ mydocimage.property.date_added }}
joshlsullivan
  • 1,375
  • 2
  • 14
  • 21
will-hart
  • 3,742
  • 2
  • 38
  • 48
6
  • first you iterate over the result
  • the images related to a Doc are retrieved by the images property of doc which is generated from the related_name attribute in the ForeignKey

code:

{% for doc in result %}
  {% for docimage in doc.images.all %}
    {{ docimage.image.url }}
  {% endfor %}
{% endfor %}
Christian Thieme
  • 1,114
  • 6
  • 6
  • With logic like this you dont even need to use foreign keys. If all the objects are in context you can loop through until the keys match. Not efficient, but good for basic structures. – Dan Walters Nov 23 '18 at 15:29