sirFunkenstine
sirFunkenstine

Reputation: 8495

Saving image to object Django

I am trying to add an image to an object. The following piece of code

event_obj = Event.objects.get_or_create(name=name, description=description, publish=publish,
                                                url=url, venue=venue_obj, image=None)

        if image_filename:
            image = None
            image_exists = False
            if image_filename != "":
                image_path = os.path.join(settings.DIRNAME, 'website', 'data', 'images', image_filename)
                if os.path.exists(image_path):
                    image_exists = True

            if image_exists:
                image_data = open(image_path, 'rb').read()
                image, created = Image.objects.get_or_create(title=name)
                event_obj.image.save(image_filename, ContentFile(image_data), True)

        event_obj.save()

is throwing the following error

event_obj.image.save(image_filename, ContentFile(image_data), True)AttributeError: 'tuple' object has no attribute 'image'

my Event model uses the following field

image = models.ImageField(null=True, blank=True, upload_to="event_images")

Can anyone explain why i cannot save the data this or maybe point me in the direction of a way that works?? Thanks!

Upvotes: 0

Views: 210

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

This has nothing to do with images.

get_or_create returns a tuple of (object, created). You've assigned the whole tuple to event_obj.

Upvotes: 1

Related Questions