Correct way to add image related info wagtail - python

I am setting up a simple site with the main purpose of having galleries of images.
Using collections to group images and display them on a specific page is how I set it up first. However I am not sure what the recommended way is to add a description.
My options I have tried are:
Custom page
This seems as the way to handle it, however I lose the nice feature of using collections
to generate pages. This would mean I separate info of a image into a page model, fragmenting the image from the data. I see that creating a custom image is recommended, but I doubt it's for this purpose.
Custom image model
This allows me to add a description field completely replaces the image model since you can only have 1 image model afaik. In a webshop like site this seems doesn't seem suitable (price, reviews, stock etc.) but since this site focuses on the image specific it seems this kind of coupling is ok.
Is the first option the way to handle this? Or how can I set this up so that a designer/user can tie data to a image without losing the base image or allow the user
to select a 'collection' with the child pages resolving a description/model related to a image.
Any tips, references or alternatives would be appreciated.
ps: I just realized this boils down to not knowing how wagtail/django does mapping db/pojo to viewmodels.

Definitely use a custom image model. This will allow you to add a description field that will be kept with the image. It will also let you extend this to add additional fields, e.g. photo credit. Your additional fields will be added to the form used to add/edit images.

Related

How can I add a changeable "subfield" to a ModelField in django?

I'm trying to create a kind of "subfield" for a CharField in django, but I'm not sure (a) if it is possible at all and (b) how to succeed if it is indeed possible.
Let's say I want a model for Tools. They would have a, e.g., a field for long_name, short_name, maybe a ForeignKey for realizing different departments. One of these tools I'd like to be a Link, the said "subfield" being a URLField with the href to the webpage.
Now, I can create multiple link entries with the associated URL, but I'd rather have only one tool called "Link" with the changing URL attached. Is this a case for ForeignKey as well? Does it make sense to have a model with only one field (well, two if you count the pkid) in it?
Or am I on a completely lost path here?
If I've understood you correctly, you want to have a number of links that can be attached to a Tool model, so instead of just having a single URLField you would have a Many-to-One relation with a Link model:
class ToolLink(models.Model):
url = models.URLField(...
class Tool(models.Model):
links = models.ForeignKey(ToolLink, ...
The problem is that you only want one particular tool to be able to hold links. Your options are to create a 'Tool' base model that then has multiple different types of tool, like 'StandardTool', 'LinkTool', etc. or to setup some logic that monitors whether the Tool has links or not (or if another tool already has links) and whether creating links is acceptable.

django-summernote attach images from another model/ForeignKey

I've added django-summernote to a project, but when the user inserts an image, instead of the upload popup, I want a popup with a ModelChoiceField for Photos.objects. It's a website for a photographer, and the Photos model has necessary additional functionality. I could have him go through the process of getting the image's url and adding it that way, but this would be much cleaner.
I've tried a lot of things, but kind of a stab in the dark all around. django-summernote docs describe how to set a custom attachment model, but only just....
First, I created another model in django-summernote.models that inherits from AbstractAttachment:
class PhotoAttachment(AbstractAttachment):
img = models.ForeignKey(Photos, on_delete=models.PROTECT, default='None')
I also tried changing django-summernote.forms.py:
from:
file = forms.ImageField(required=True)
to:
img = forms.ModelChoiceField(queryset=Photos.objects, required=True)
...and a few other things. I've been poking around for a few hours, but I honestly don't know where to begin. I do have working ModelChoiceFields on ForeignKey's elsewhere in my models coming up in the old djangoadmin, so that's all fine - I just want to customize summernote to pull one up when the user tries to add an image. Suggestions?

Wagtail large list within stream field block

We use wagtail for our blogs that are part of an eCommerce site. When we want to add a product to the blog as it stands we have to put the exact product name in which then matches on save. As names can change this then breaks that blog which isnt ideal.
What we would like to do is add a field to our streamfield block that lets you pick from our list of products, however as we have 200k+ products and there might be up to 20 products on each blog loading the list into a dropdown is no good. What we need is to replicate what we do in Django admin using django-autocomplete-light where you start typing and get results based on that rather than loading the list into the HTML.
What we cant work out is how to do that within a streamfield block, I have seen libraries like "Wagtail Autocomplete" however it seems you can only use that as a panel rather than within a block. As far as we can see you can only use sub-classes of wagtail.core.blocks or they dont show up in the interface.
Any ideas?
Thanks in advance!
You can extend the editor. Although the examples are mainly focussed on adding CSS style, it still should be possible. You can read more on this at the documentation: http://docs.wagtail.io/en/v2.8/advanced_topics/customisation/extending_draftail.html#
Another option would be is to use the Wagtail autocomplete panel for specifying the product you need to reference to, in a seperate field of course. Then, using a regular f-string replacement, place the proper product details in the text.

Django filer image related to another model, i.e. is it being used?

I am creating a cleanup script for Django Filer, so that if an image is not in use, e.g. it's not related to any other model, then I'd like to delete the image.
I can't figure out how to detect if the image is related to any other objects though. Does anybody know how to achieve this?
You can view this information when you press the delete button and are on the delete confirmation page, but I'm not sure how Django has done this.
Any help would be much appreciated.
Edit
To clarify,
from filer.models import Image
i = Image.objects.all().first()
i.get_all_related_objects.count()
The last line of the above is not valid, but this is what I'm trying to achieve, so if this is 0, then I can remove the file.
You can get all related objects, no matter from what related model they come, using
instance._meta.get_all_related_objects()
so when assuming you have an Image model:
for image in Image.objects.all():
if not image._meta.get_all_related_objects(): # if this list is empty, there are no relations pointing here
image.delete()

Django's Admin - Many-to-Many field confusion

I'm teaching myself Django and creating a personal portfolio site. I've setup a model called FolioItem which makes use of a Many-To-Many relation to another model called FolioImage. The idea being each FolioItem can have numerous FolioImages.
class FolioImage(models.Model):
image = models.FileField(upload_to='portfolio_images')
def __unicode__(self):
return self.image.name
class FolioItem(models.Model):
title = models.CharField(max_length=50)
thumbnail = models.FileField(upload_to='portfolio_images')
images = models.ManyToManyField(FolioImage)
def __unicode__(self):
return self.title
In Django's admin interface I can create new FolioItem's but the images field (the many-to-many relating to a FolioImage) is showing all existing FolioImage's added previously to other FolioItems.
Really this should be blank as it is a new item and doesn't have any images associated yet. Is there any way to force this selector to be blank when creating a new item?
Also, it seems when I attempt to edit an existing entry I can't add or delete FolioImage associations (the small green + icon is not present).
Any advice would be great.
I think you are using the wrong relationship here. If you only want FolioImages to belong to a single FolioItem, then you should be using a ForeignKey from FolioImage to FolioItem. You can then configure the admin interface to show the related images for an item as inline formsets, which will allow you to add an edit only the images for that item.
Is there any way to force this
selector to be blank when creating a
new item?
I think you are slightly confused about the interface to the default select multiple widget. It is showing you all the existing images, making them available to you as choices, but there is no relationship on a brand new item. You have to actually click on an item, or do the shift (or control)-click trick to select multiple items, then save your folio item in order to make the relationship.
Also, it seems when I attempt to edit
an existing entry I can't add or
delete FolioImage associations (the
small green + icon is not present).
Again, I think you just need to click or ctrl-click to select/deselect the images you want to associate with your item.
You may also be interested in using Django's cool horizontal or vertical filter instead of the default select multiple interface. You can also use the inline interface. More details are here and here. I think the filter interface is much more intuitive.

Categories