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()
Related
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'
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
Hiii every one.
I am new to Django and start to create a basic project
and now i have a problem with ImageField and Upload_To
i have a model that have some field, like (car_name) and (photo) and i called that "Cars"
admin can create a new car and select an image
and
type a name for that car
then admin hit save button
and now image should save somewhere
I want to save that image into this path:
photos/Cars/???car_name???
my question is: how get the name of car and use that name to use in ((upload_to = path))?
im really sorry if i have writing issue 🙏🙏🙏
This is my code:
class Cars(models.Model):
car_name= models.CharField(max_length = 40)
photo = models.ImageField(upload_to=f"photos/Cars/{car_name}")
def get_upload_path(instance, filename):
return os.path.join(instance.first_name, filename)
class Cars(models.Model):
car_name= models.CharField(max_length = 40)
photo = models.ImageField(upload_to=get_upload_path)
this worked to me
I have a model(Project) that contains ManyToManyField to another model(Screenshot). I want to create a view inheriting CreateAPIView such that it is able to serialize data for creating an instance of project model including the Screenshot model from the same view.
models.py
class Screenshot(models.Model):
image = models.ImageField(default='screenshots.jpg',upload_to='screenshots')
class Project(models.Model):
team=models.ForeignKey(Team,on_delete=models.CASCADE)
name=models.CharField(max_length=100,null=True,blank=True)
theme = models.ForeignKey(Theme,on_delete=models.CASCADE,null=True,blank=True)
desc=models.TextField()
accepted = models.BooleanField(default=False)
git_url=models.URLField(null=True,blank=True)
youtube_url=models.URLField(null=True,blank=True)
screenshots=models.ManyToManyField(Screenshot)
serializers.py
class ScreenshotCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Screenshot
fields=['image']
class ProjectCreateSerializer(serializers.ModelSerializer):
image_1 = ScreenshotCreateSerializer()
image_2 = ScreenshotCreateSerializer()
image_3 = ScreenshotCreateSerializer()
image_4 = ScreenshotCreateSerializer()
image_5 = ScreenshotCreateSerializer()
class Meta:
model = Project
fields = ['name','theme','desc','git_url','youtube_url','image_1','image_2','image_3','image_4','image_5']
def create(self,validated_data):
try:
image1 = validated_data.pop('image_1')
image2 = validated_data.pop('image_2')
image3 = validated_data.pop('image_3')
image4 = validated_data.pop('image_4')
image5 = validated_data.pop('image_5')
except:
pass
print(validated_data)
test_team_obj = Team.objects.all()[0]
project_obj = Project(**validated_data)
project_obj.team = test_team_obj
project_obj.save()
if (image1):
project_obj.screenshots.create(image=image1.get('image'))
if (image2):
project_obj.screenshots.create(image=image2.get('image'))
if (image3):
project_obj.screenshots.create(image=image3.get('image'))
if (image3):
project_obj.screenshots.create(image=image4.get('image'))
if (image4):
project_obj.screenshots.create(image=image5.get('image'))
return project_obj
The form is displayed as expected with 5 image fields. After submitting the form, the required project instance is created, screenshot instances are created as well and the just created screenshot instances are linked to that just created project instance. Everything runs well. But an error arises saying:
Got AttributeError when attempting to get a value for field image_1 on serializer ProjectCreateSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the Project instance.
Original exception text was: 'Project' object has no attribute 'image_1'.
Regardless of the error, the data is well updated on the database as desired. But the error is what killing me! I even tried to use a single image field instead of 5 and put many=True but it says:
Lists are not currently supported in HTML input
sorry if this is an obvious question but I have been searching for a few days and have not been able to come up with a result.
I am creating a simple photo gallery app. There are four galleries, each containing a photo (the photo consists of a 'before' image, 'after' image and caption). I am trying to use django-admin to allow users to click on a gallery and then add photos.
I am using a TabularInline to edit the photos within each gallery. In addition to the default columns on the TabularInline, I would like to add a column that shows a thumbnail preview of the 'before' photo and 'after' photo (I am using easy-thumbnails for this). After much searching, it seems like the best way to do this is to override the django-admin tabularInline.html template and add the column myself - so I created another copy and am trying to edit it now.
What I would like to do is simply reference the Photo object within the Django admin template that I am overriding - but I don't know the appropriate tag to use. I need the reference so I can use it in conjunction with the easy-thumbnails thumbnail tag ... but for the life of me I cannot figure out the template tag that references the object. I have tried iterating through the ModelForm, FormSet, and FieldSet objects but none seem to give me a direct reference to the object.
# models.py
class Gallery(models.Model):
name = models.CharField(max_length=200)
url = models.CharField(max_length=200)
desc = models.TextField()
def __unicode__(self):
return self.name
class Photo(models.Model):
gallery = models.ForeignKey(Gallery)
before = models.ImageField(upload_to='gallery')
after = models.ImageField(upload_to='gallery')
caption = models.CharField(max_length=1000)
order = models.IntegerField(blank = True, null = True)
def __unicode__(self):
return "Photo " + str(self.order)
# admin.py
class GalleryForm(forms.ModelForm):
model = Gallery
class Media:
js = (
'/assets/js/jquery-1.4.2.min.js',
'/assets/js/jquery-ui-1.8.2.custom.min-admin-sortable.js',
'/assets/js/menu-sort.js',
)
class PhotoInline(admin.TabularInline):
model = Photo
extra = 1
template = "admin/tabular-thumbnails.html"
admin.site.register(Gallery,
inlines=[PhotoInline],
form = GalleryForm)
Thanks so much in advance and please let me know if there's any additional information I can offer. I am using Django 1.1
In Django 2.1 {{adminform.form.instance.field_name}} worked for me.
{{ form.instance }} will always be the model instance associated with a modelform, assuming there is one.
(Note that ``{{ formset.instance }}` is the instance of the parent model in an inline formset).