in django set default image to ImageField as Default object - django

I have a model in the model and Imagefield is there.
class Job(models.Model):
title = models.CharField(max_length=200)
image = models.ImageField(upload_to=" jobOpeningImages", default=' jobOpeningImages/1_oKH7Co9.jpeg', blank=True)
But I am not getting any current image URL in the localhost.
I am expecting this type of result
So how can we achieve the below? in first image
Currently: jobOpeningImages/1_oKH7Co9.jpeg

Related

Django - ImageField with multiple images

I have a model that has an ImageField. I want users to be able to upload multiple images for an object of the model - not only a single image. How can this be done? Whether with and image field or another approach.
You cannot store several images in one ImageField.
One solution for this problem would be to create an additional model (I called it "Attachment" for my social network pet project, call your's whatever should suit you) and have it reference the original model in a Foreign key. That way you can upload as many images as you want and create an instance of that new model for each new image.
Example Attachment model:
class Attachment(DatetimeCreatedMixin, AuthorMixin):
class AttachmentType(models.TextChoices):
PHOTO = "Photo", _("Photo")
VIDEO = "Video", _("Video")
file = models.ImageField('Attachment', upload_to='attachments/')
file_type = models.CharField('File type', choices=AttachmentType.choices, max_length=10)
publication = models.ForeignKey(TheOriginalModelYouUsedImageFieldIn, on_delete=models.CASCADE, verbose_name='Model that uses the image field')
class Meta:
verbose_name = 'Attachment'
verbose_name_plural = 'Attachments'

How can I thumbnail images in serialization of Django REST Framework?

I recently jumped into Django REST Framework. Before using it, I thumbnailed images using django-imagekit. Like you see the models below, it worked well, so I used original size images from image and thumbnailed size images from image_thumbnail.
models.py
class Image(models.Model):
...
image = ProcessedImageField(null=True, blank=True, upload_to=image_path,
processors=[Thumbnail(1000, 1400)], format='JPEG')
image_thumbnail = ImageSpecField(
source='image', format='JPEG', options={'quality': 40})
...
The problem is I can't use image_thumbnail in my serializers. I can use image, but image_thumbnail throws an error message A server error occurred. Please contact the administrator.
serializers.py
class ImageRandomSerializer(ModelSerializer):
class Meta:
model = Image
fields = ('image', 'image_thumbnail', )
Can I not thumbnailed images from models.py in serializers.py? Should I thumbnail them with some Django REST Framework thumbnail tool?
UPDATE
After setting DEBUG=True, it throws the error 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte.
I just found the answer from here.
Added image_thumbnail = ImageField(read_only=True), and now it's working well.
from rest_framework.serializers import ImageField
class ImageRandomSerializer(ModelSerializer):
store = StoreDomainKeySerializer()
image_thumbnail = ImageField(read_only=True)
class Meta:
model = Image
fields = ('store', 'image', 'image_thumbnail',)

Django - how can I set default image for an ImageField model

I have an imagefield type for my model
class Attendance(models.Model):
face_image = models.ImageField()
the question is if I have an image in static, how can i assign it to this imagefield ? if not it could be from a url the image, how can i assign to this imagefield
For example this would be how I would create the object, what should be assigned to face_image_obj ?
attendance_new = Attendance(face_image = face_image_obj)
attendance_new.save()

django admin foreign key to file field and many to many field

I have a model Teacher which has following data:
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ForeignKey(Thumbnail)
class Thumbnail(models.Model):
thumbnail = models.FileField(upload_to=upload_to)
class TeacherAdmin(admin.ModelAdmin):
list_display = ('name',)
Here I want to relate all the thumbnail in my project to Thumbnail field. In my admin it shows me a dropdown of thumbnail while I want to add the thumbnail from admin.
Is it possible that I can upload from admin too?
Also what can I do in the case like :
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ManyToManyField(Thumbnail)
If I want to add multiple Images.
The only issue I am having is on admin side. Client side can be handles from regular forms.
Need suggestions and Ideas

Django Model store image file size

I am using Django 1.6. I have a model for uploading image files that looks like this.
class Image(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
url = models.ImageField(upload_to=get_image_path,
null=True,
blank=True,
height_field = 'height',
width_field = 'width',
verbose_name='Image')
height = models.IntegerField(blank=True)
width = models.IntegerField(blank=True)
size = models.IntegerField(blank=True)
format = models.CharField(max_length=50)
caption = models.CharField(max_length=255)
def clean(self):
self.size = self.url.size
class Meta:
db_table = 'image'
As you can see, I am storing the size of the image when the clean() method is called. This works for what I want to do, but is this best practise? Is there a better way to automatically save the image's file size when saving?
The second part of my question is how can I get the content type as well?
Thanks,
Mark
Model.clean() should be used for validation - do not use it to update/save the data, but rather use it to correct any invalid data (or throw an exception/error message).
You may want to consider not even storing the size of the image in the database, given that you can access it from the ImageField - it eliminates the possibility of the data becoming inconsistent as it changes over time.
I believe this question/answer should address your second question.
For the first question
Check out the Python Imaging Library PIL on this thread.
from PIL import Image
im=Image.open(filepath)
im.size # (width,height) tuple
For the second question
HttpRequest.META, more specifically HttpRequest.META.get('CONTENT_TYPE')
from this thread