Django, select image from previous uploaded images - django

I have a Post model which has a ImageField.
class Post(models.Model):
headline = models.CharField(max_length=255)
lead = models.TextField(blank=True, null=True)
body = models.TextField(blank=True, null=True)
image = models.ImageField(upload_to="posts", blank=True, null=True, max_length=255)
In my ModelForm, the file input for the image works perfectly, but I was wondering how to let the user choose between uploading a new image, or selecting one from all the images that have been previously uploaded to the site.
Any pointers?

In order to do that define another model Image and then create a foreign key from Post to Image. When creating a new post, you can either select an existing image or upload a new one and then select it.

Related

Confusion about Imagefield

I am learning Django as a beginner. Commonly speaking(documentation-wise), is Imagefield for photos? What field is then for the image files? (dmg for example). Thank you.
ImageField inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image using Pillow. It also has two additional optional arguments: height_field and width_field (these are auto-populated with the height and width of the image respectively, each time the model instance is saved).
Example:
class Photo(models.Model):
image = models.ImageField(blank=True, null=True, upload_to='image_collection', height_field='image_height', width_field='image_width', max_length=1000)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)

Django: ManyToMany association not working

models.py
class Gallery(models.Model):
name = models.CharField(max_length=100)
class Image(models.Model):
in_gallery = models.ManyToManyField(Gallery, blank=True)
title = models.CharField(max_length=100)
class ImageInGallery (models.Model):
image = models.ForeignKey(Image, blank=True, null=True, on_delete=models.DO_NOTHING)
gallery = models.ForeignKey(Gallery, blank=True, null=True, on_delete=models.DO_NOTHING)
Click to view the screenshot of my admin page of adding a Gallery
Click to view the screenshot of my admin page of adding an Image
Have a look at the screen shot....
(ignore the fields that I didn't show in models.py code for simplicity) See the difference I have in admin page of adding a Gallery and Image..... I cannot add image to Gallery
How to fix this?
what's the purpose of ImageInGallery ?
You have to use through argument.
Since you define ManyToManyField in Image you already have a bilateral relation.
If you want all galleries related to an image1 instance of Image you can do a :
image1.galleries.all()
If you want images related to gallery1 instance of Gallery, you can do :
galleries.image_set.all()
In documentation, your Gallery is 'Publication' and Image is 'Article'.

Django FileBrowseField upload form

I have an Artwork and ArtworkPhoto models in Django.
ArtworkPhoto has an FK to the Artwork and stores some additional data on the photo.
class ArtworkPhoto(models.Model):
title = models.CharField(max_length=200, verbose_name='Omschrijving', blank=True)
photo = FileBrowseField("Image", max_length=200, directory="artwork/",
extensions=[".jpg", ".png"], blank=False, null=False)
artwork = models.ForeignKey(Artwork, null=False, verbose_name='kunstwerk')
Every Artwork has X photos, all working fine.
Now I want to provide authenticated visitors with an upload form to add an artwork and photo's.
How can I save a posted file to the ArtworkPhotoModel? It's a Grapelli FileBrowser > Filebrowsefield ...
Thanks, i'm a bit stuck here. ...

Add multiple objects of same model at the same time in the django admin

I have a model to upload pictures:
class Snap(models.Model):
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=150, blank=True, null=True)
pubdate = models.DateTimeField(default=timezone.now)
In the admin it contains all the Snap's field (i.e. one field to upload an image ,its caption and the pubdate) which is as expected. How do I add multiple record of Snap's in one go. I hope I was clear, if not please ask. Your help and guidance will be very much appreciated. Thank you.
admin.py:
class SnapAdmin(admin.ModelAdmin):
class Meta:
model = Snap
admin.site.register(Snap, SnapAdmin)

Use already uploaded image in Django's ImageField

Is there an option in ImageField to set the path or an already uploaded image?
My use case is as follows: I want to reutilize the same image in two different content entries. I don't want to upload the image twice or to store two copies of the same image.
Just assign the image field from one model instance to another:
original_entry = Entry.objects.get(pk=1)
another_entry = Entry.objects.get(pk=2)
another_entry.image = original_entry.image
another_entry.save()
new_entry = Entry.objects.create(image=original_entry.image)
Just put an image in it's own model:
class Image(models.Model):
image = models.ImageField(...)
class ModelA(models.Model)
image = models.ForeignKey(Image, null=True, blank=True)
class ModelB(models.Model)
image = models.ForeignKey(Image, null=True, blank=True)