python - Django how to get in model the value from another model -
how in django models class value of blogpagegalleryimage image or foreign key or parental key?
class blogpage(page): date = models.datefield("Дата публикации" , default=datetime.date.today ) body = richtextfield(blank=true) name = models.charfield(max_length=80, default='Лучший') class blogpagegalleryimage(orderable): page = parentalkey(blogpage, related_name='gallery_images') image = models.foreignkey('wagtailimages.image', on_delete=models.cascade, related_name='+') caption = models.charfield(blank=true, max_length=250) panels = [ imagechooserpanel('image'), fieldpanel('caption'), ] class another(blogpage): page = parentalkey(blogpage, related_name='instagram_post') image = blogpagegalleryimage.objects.get(blogpagegalleryimage.) caption = models.charfield(blank=true, max_length=250) panels = [ imagechooserpanel('image'), fieldpanel('caption'), ]
it seems looking access blog page images in blogpage
, another
template. can achieved adding blog page images rendering context. override get_context
in blogpage
method:
def get_context(self, request, *args, **kwargs): context = super(blogpage, self).get_context( request, *args, **kwargs ) context['images'] = blogpagegalleryimage.objects.filter( page=self, ) return context
this means can in template access images using {{ images }}
. example:
{% in images %} <div class="image"> {% image i.image %} <p>{{ i.caption }}</p> </div> {% endfor %}
Comments
Post a Comment