Confusion about Imagefield - django

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)

Related

Django, select image from previous uploaded images

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.

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. ...

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)

Django CMS Custom plugin - how to handle translations

I have a carousel which consists of a Gallery model that has several Picture models. The Picture model looks like this:
class Picture(models.Model):
gallery = models.ForeignKey(Gallery)
image = models.ImageField(upload_to="uploads/images/")
title = models.CharField(max_length=30, null=True, blank=True)
tagline = models.CharField(max_length=30)
description = models.CharField(max_length=100)
page_link = PageField(related_name="Page", null=True, blank=True)
As the website has 3 languages, how can the admin use the same carousel for all languages, having the same image, but different translatetions for texts?
There has to be a better way than just using title_en, title_de, etc, right?
I would highly recommend django-hvad.
Here's your models.py as an hvad TranslatableModel:
class Picture(TranslatableModel):
translations = TranslatedFields(
title = models.CharField(max_length=30, null=True, blank=True)
tagline = models.CharField(max_length=30)
description = models.CharField(max_length=100)
)
gallery = models.ForeignKey(Gallery)
image = models.ImageField(upload_to="uploads/images/")
page_link = PageField(related_name="Page", null=True, blank=True)
def __unicode__(self):
return self.lazy_translation_getter('title', self.pk)
As mentioned in the docs, this app is still in Beta but I've used it in many production environments and works good.
One thing to keep in mind is that it does bring a few limitations.
The way it works is it creates a model called PictureTranslation (or something like that) which will have your three fields title, tagline, description and a ForeignKey to Picture.
You can still do picture_instance.title as long as the picture_instance is language aware.
If you would like, I can give a better explanation based on your specific use case.

Django - how to make ImageField/FileField optional?

class Product(models.Model):
...
image = models.ImageField(upload_to=generate_filename, blank=True)
When I use ImageField(blank=True) and do not select image into admin form, an exception occurs.
In django code you can see this:
class FieldFile(File):
....
def _require_file(self):
if not self:
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
def _get_file(self):
self._require_file()
...
Django trac has ticket #13327 about this problem, but seems it can't be fixed soon. How to make these field optional?
blank=True should work. If this attribute, which is False by default, is set to True then it will allow entry of an empty value.
I have the following class in my article app:
class Photo(models.Model):
imagename = models.TextField()
articleimage = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
I make use of the above class in another class by using the ManyToManyField relationship:
class Article(models.Model):
pub_date = models.DateTimeField(default=timezone.now)
slug = models.SlugField(max_length=130)
title = models.TextField()
photo = models.ManyToManyField(
Photo, related_name='photos', blank=True)
author = models.ForeignKey(User)
body = models.TextField()
categories = models.ManyToManyField(
Category, related_name='articles', null=True)
I want to make images in my articles optional, so blank=True in
photo = models.ManyToManyField(Photo, related_name='photos', blank=True)
is necessary. This allows me to create an article without any images if I want to.
Are you using class Product in any relationship? If so, make sure to set blank=True in the relationship you are using.
Set null=True (see documentation)
class Product(models.Model):
...
image = models.ImageField(upload_to=generate_filename, blank=True, null=True)
If 'optional' means that you want to be able to disregard the image field altogether. Then blank=True, so do the trick. However, if you are still getting the same error, then this means that you are using it's url either in the template somewhere or you are trying to open the path of the image in models.py or views.py ( may be to resize the image or some other backend preprocessing).
That is why it is always suggested to use try - catch block while handling files.